I have maybe a very naive question (I am no expert in C programming), but I couldn't get a fully satisfactory explanation. Here is just the declaration of static array and a few prints:
#include <stdlib.h>
#include <stdio.h>
void main() {
int N=3, a[N];
for (int i=0; i<N; i++) a[i] = 1000+i;
printf("&a = %p\n",&a);
printf("a = %p\n",a);
printf("*a = %d\n",*a);
printf("*(&a) = %d (as an int)\n",*(&a));
printf("*(&a) = %p\ (as a pointer)\n",*(&a));
}
The output is:
&a = 0x7ffee9043ae0
a = 0x7ffee9043ae0
*a = 1000
*(&a) = -319989024 (as an int)
*(&a) = 0x7ffee9043ae0 (as a pointer)
Since &a
and a
are identical, showing the same address in memory, I was first expecting *(&a)
and *a
being identical as well, both equal to 1000
.
Then I thought about the types: a
is apparently considered as an int*
, so &a
is a int**
. It turns that *a
is an int
, while *(&a)
is an int*
: they are not of the same type, the latter is a pointer.
It makes sense... But my question is then: why are &a
and a
identical in the first place?