This Q in C, how come the address of array, &array, &array[0] are the same? made me investigate on pointers and arrays: I confirm they are very similar but not identical.
&a[0]
is the verbose form for a
or &a
. But what about the precedence of &
vs []
?
When I put parens the wrong way like (&a)[1]
this changed the value, but not (&a)[0]
.
#include <stdio.h>
int main(){
int a[3] = {10,20,30};
int *b;
b = a;
printf("%p a\n", a);
printf("%p a+1\n", a+1);
printf("%p a+2\n\n", a+2);
printf("%p (&a)[0]\n", (&a)[0]);
printf("%p (&a)[1]\n", (&a)[1]);
printf("%p (&b)[0]\n", (&b)[0]);
printf("%p (&b)[1]\n\n",(&b)[1]);
printf("%p cast\n\n", (void **)((&a) + 1) );
int *c = a;
printf("%p\n", c);
printf("%p\n", &c);
return 0;
}
output:
0x7ffe18053c9c a
0x7ffe18053ca0 a+1
0x7ffe18053ca4 a+2
0x7ffe18053c9c (&a)[0]
0x7ffe18053ca8 (&a)[1]
0x7ffe18053c9c (&b)[0]
(nil) (&b)[1]
0x7ffe18053ca8 cast
0x7ffe18053c9c c (int *c = a)
0x7ffe18053c90 &c
I found (void **)((&a) + 1)
to get the same result. Can't leave out a piece it seems. Suddenly &a
does create a pointer, anonymously.
With index zero you get the base address (...c9c) both with a
and its pointer b
. But with index 1 strange things happen, and differently for b
.
Is there a rule for this? I don't even know if I want to hear it! Is that what &a
does besides being between a
and &a[0]
?
good A here by chux. His cast is simpler, but does it do the same?
difference between &array and &array[0] in C
From mentioned link I not only simplified my cast but now I think I know what it does and why the index or offset of 0 is special.
(void *)(&a + 0)
is the cast. But why even cast? &a + 0
prints the base address. With offset + 1
the address advances a whole array size (not a pointer as I tried) - "one past the end". A second dimension. The base remains, but the offset changes.
For assignment:
int *c = a;
int (*d)[3] = &a; // with some help from the left...
int *e = (int *)&a; // ... or on the right side...
int *f = &a; // or with a warning, but working
So (&a)
generates a new type, but not a pointer? New reference to the same spot. Address of = pointer to = array of.