How does an array appear to hold a pointer value and an element value at the same location in memory? [duplicate]

Solution 1:

The array you declared

intArr[4];

in fact is a named extent of memory. So the address of the array and the address of its first element is the address of the extent of memory occupied by the array.

The only difference between the expressions &intArr and intArr used in the calls of printf is their types. The first expression has the pointer type int ( * )[4] and the second expression has the pointer type int * but the values produced by the expressions are equal each other.

In this call

printf("%p\n", intArr,);

the array designator is implicitly converted to a pointer to its first element.

Arrays used in expressions with rare exceptions as for example using them in the sizeof operator are implicitly converted to pointers to their first elements.

For example if you will write

printf( "%zu\n", sizeof( intArr ) );

you will get the size of the array.

On the other hand, if you will write

printf( "%zu\n", sizeof( intArr + 0 ) );

you will get the size of a pointer because in this expression intArr + 0 the array designator is implicitly converted to a pointer to its first element.

As for these expressions intArr[0] and *intArr then the first expression is evaluated like *( intArr + 0 ) that is the same as *intArr.