Differences between arrays, pointers and strings in C -


just have question in mind troubles me.

i know pointers , arrays different in c because pointers store address while arrays store 'real' values.

but i'm getting confused when comes string.

char *string = "string"; 

i read line several things :

an array of chars created compiler , has value string.

then, array considered pointer , program assigns pointer string pointer points first element of array created compiler.

this means, arrays considered pointers.

so, conclusion true or false , why ?

if false, differences between pointers , arrays ? thanks.

a pointer contains address of object (or null pointer doesn't point object). pointer has specific type indicates type of object can point to.

an array contiguous ordered sequence of elements; each element object, , elements of array of same type.

a string defined "a contiguous sequence of characters terminated , including first null character". c has no string type. string data layout, not data type.

the relationship between arrays , pointers can confusing. best explanation know of given section 6 of comp.lang.c faq. important thing remember arrays not pointers.

arrays in sense "second-class citizens" in c , c++. cannot assigned, passed function arguments, or compared equality. code manipulates arrays using pointers individual elements of arrays, explicit mechanism specify how long array is.

a major source of confusion fact expression of array type (such name of array object) implicitly converted pointer value in contexts. converted pointer points initial (zeroth) element of array. conversion not happen if array either:

  • the operand of sizeof (sizeof array_object yields size of array, not size of pointer);
  • the operand of unary & (&array_object yields address of array object whole); or
  • a string literal in initializer used initialize array object.

    char *string = "string";

to avoid confusion, i'm going make few changes in example:

const char *ptr = "hello"; 

the string literal "hello" creates anonymous object of type char[6] (in c) or const char[6] (in c++), containing characters { 'h', 'e', 'l', 'l', 'o', '\0' }.

evaluation of expression, in context, yields pointer initial character of array. pointer value; there no implicitly created pointer object. pointer value used initialize pointer object ptr.

at no time array "treated as" pointer. array expression converted pointer type.

another source of confusion function parameters appear of array type of pointer type; type adjusted @ compile time. example, this:

void func(char param[10]); 

really means:

void func(char *param); 

the 10 silently ignored. can write this:

void print_string(char s[]) {     printf("the string \"%s\"\n", s); } // ... print_string("hello"); 

this looks manipulating arrays, in fact array "hello" converted pointer, , pointer what's passed print_string function.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -