Interview Question
Country: United States
arrays are special types which represents a continuous storage location . here you are making a pointer to an array , which would actually concider the first element i.e myarray [0] as its value
. when you deference *myarray it actually goes to the "myarray[0]" address which may be valid or invalid ..
i think linker should have complained i dont know ..
the confusion occurs because these statement works perfectly
int array[10];
int *ptr = array ;
ptr[0] = 10 ;
The code may give segmentation fault or linking error.
int array[10]; tells that array is an array of 10 integers.
extern int*myarray; tells that myarray is a pointer to an integer which in defined at some other, place probably in some other .c file. But it is not defined any where(even not initiallised).
When both file links during execution extern int *myarray declaration in file2.c will search for *myarray declared and allocated space ,and that is done in file1.c myarray[10].So what exactly you are saying is *myarray will not match to myarray[].
extern is used outside.It should be used inside code.
- vineetsetia009 May 11, 2012