I know this topic has been addressed a few times but I still don't get it.
Referring to http://c-faq.com/aryptr/aryptr2.html
char a[] = "hello";
char *p = "world";
creates an array "a". The variable is only a label of the address of the first memory address. p is a variable (== label of another memory address), containing the address of the first character (w - which may be somwhere else in the memory).
The thing I don't understand is the example from this topic (thanks!): using extern to link array with pointer :
extern1.c
extern int *array;
int test();
int main(int argc, char *argv[])
{
printf ("in main: array address = %x\n", array);
test();
return 0;
}
extern2.c
int array[10] = {1, 2, 3};
int test()
{
printf ("in test: array address = %x\n", array);
return 0;
}
Why does the pointer variable "extern int *array" (== a label of memory address, containing an other address) in extern1 already contain the content of array[0]?
What is the difference to
int array[10] = {1, 2, 3};
int *ptrArrWorking = array; // == &array[0];
? In the extern case it would be something like
int array[10] = {1, 2, 3};
int *ptrArrNotWorking = array[0];
edit: to be more explicit let's interpret the example above as
int array[10] = {1, 2, 3};
int *ptrArrNotWorking = (int *)array[0];
so this simulates the same behaviour which can be seen in the extern example.
Where is the indirection hidden?
Thank you so much.