Reference to Array vs reference to array pointer Reference to Array vs reference to array pointer c c

Reference to Array vs reference to array pointer


The problem is, when we do &array, we are getting a char (*)[10] from an char [10], instead of a char **.

Before we do our experiment, I will emphasize that, when we pass an array as an argument to a function, C actually casts the array to a pointer. The big bucket of data is not copied.

Thus, int main(int argc, char **argv) is identical to int main(int argc, char *argv[]) in C.

This made it available for us to print the address of an array with a simple printf.

Let's do the experiment:

char array[] = "john";printf("array:  %p\n", array);printf("&array: %p\n", &array);// Output:array:  0x7fff924eaae0&array: 0x7fff924eaae0

After knowing this, let's dig into your code:

char array[10] = "john";char *bla = array;check(&bla);check(&array);

bla is char *, and &bla is char **.

However, array is char [10], and &array is char (*)[10] instead of char **.

So when you pass &array as an argument, char (*)[10] acts like a char * when passing as an argument, as is said above.

Therefore **(char **) &bla == 'j' while *(char *) &array == 'j'. Do some simple experiments and you will prove it.

And you are casting void *elemAddr to a char ** and try to deference it. This will only work with &bla since it is char **. &array will cause a segfault because "john" is interpreted as an address as you do the cast.


For check(&bla); you are sending pointer to pointer

void check(void* elemAddr){    char* word = *((char**)elemAddr);  // works fine for pointer to pointer    printf("word is %s\n",word);}

This is working fine.

But, for check(&array); you are passing pointer only

void check(void* elemAddr){    char* word = *((char**)elemAddr);  // This is not working for pointer     char* word = *(char (*)[10])(elemAddr);   // Try this for [check(&array);]    printf("word is %s\n",word);}

Full Code--

Code for check(array);:

void check(void* elemAddr){    char* word = *(char (*)[10])(elemAddr);    printf("word is %s\n",word);}int main() {   char array[10] = {'j','o','h','n'};    check((char*)array);  return 0;}

Code for check(&bla);:

void check(void* elemAddr){    char* word = *((char**)elemAddr);    printf("word is %s\n",word);}int main() {   char array[10] = {'j','o','h','n'};   char* bla = array;   check(&bla);  return 0;}


The C specification says that array and &array are the same pointer address.

Using the name of an array when passing an array to a function will automatically convert the argument to a pointer per the C specification (emphasis mine).

6.3.2.1-4

Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.

So calling func(array) will cause a pointer to char[] to be passed to the function. But there is a special case for using the address-of operator on an array. Since array has type "array of type" it falls into the 'Otherwise' category of the specification (emphasis mine).

6.5.3.2-3

The unary & operator yields the address of its operand. If the operand has type ‘‘type’’, the result has type ‘‘pointer to type’’. If the operand is the result of a unary * operator, neither that operator nor the & operator is evaluated and the result is as if both were omitted, except that the constraints on the operators still apply and the result is not an lvalue. Similarly, if the operand is the result of a [] operator, neither the & operator nor the unary * that is implied by the [] is evaluated and the result is as if the & operator were removed and the [] operator were changed to a + operator. Otherwise, the result is a pointer to the object or function designated by its operand

So calling func(&array) will still cause a single pointer to be passed to the function just like calling func(array) does since both array and &array are the same pointer value.

Common-sense would lead you to believe that &array is a double pointer to the first element of the array because using the & operator typically behaves that way. But arrays are different. So when you de-reference the passed array pointer as a double pointer to the array you get a Segmentation fault.