They're not identical pointers. They're pointers of distinct types that all point to the same memory location. Same value (sort of), different types.
A 2-dimensional array in C is nothing more or less than an array of arrays.
The object a
is of type int[2][2]
, or 2-element array of 2-element array of int
.
Any expression of array type is, in most but not all contexts, implicitly converted to ("decays" to) a pointer to the array object's first element. So the expression a
, unless it's the operand of unary &
or sizeof
, is of type int(*)[2]
, and is equivalent to &a[0]
(or &(a[0])
if that's clearer). It becomes a pointer to row 0 of the 2-dimensional array. It's important to remember that this is a pointer value (or equivalently an address), not a pointer object; there is no pointer object here unless you explicitly create one.
So looking at the several expressions you asked about:
&a
is the address of the entire array object; it's a pointer expression of type int(*)[2][2]
.
a
is the name of the array. As discussed above, it "decays" to a pointer to the first element (row) of the array object. It's a pointer expression of type int(*)[2]
.
*a
dereferences the pointer expression a
. Since a
(after it decays) is a pointer to an array of 2 int
s, *a
is an array of 2 int
s. Since that's an array type, it decays (in most but not all contexts) to a pointer to the first element of the array object. So it's of type int*
. *a
is equivalent to &a[0][0]
.
&a[0]
is the address of the first (0th) row of the array object. It's of type int(*)[2]
. a[0]
is an array object; it doesn't decay to a pointer because it's the direct operand of unary &
.
&a[0][0]
is the address of element 0 of row 0 of the array object. It's of type int*
.
All of these pointer expressions refer to the same location in memory. That location is the beginning of the array object a
; it's also the beginning of the array object a[0]
and of the int
object a[0][0]
.
The correct way to print a pointer value is to use the "%p"
format and to convert the pointer value to void*
:
printf("&a = %p
", (void*)&a);
printf("a = %p
", (void*)a);
printf("*a = %p
", (void*)*a);
/* and so forth */
This conversion to void*
yields a "raw" address that specifies only a location in memory, not what type of object is at that location. So if you have multiple pointers of different types that point to objects that begin at the same memory location, converting them all to void*
yields the same value.
(I've glossed over the inner workings of the []
indexing operator. The expression x[y]
is by definition equivalent to *(x+y)
, where x
is a pointer (possibly the result of the implicit conversion of an array) and y
is an integer. Or vice versa, but that's ugly; arr[0]
and 0[arr]
are equivalent, but that's useful only if you're writing deliberately obfuscated code. If we account for that equivalence, it takes a paragraph or so to describe what a[0][0]
means, and this answer is probably already too long.)
For the sake of completeness the three contexts in which an expression of array type is not implicitly converted to a pointer to the array's first element are:
- When it's the operand of unary
&
, so &arr
yields the address of the entire array object;
- When it's the operand of
sizeof
, so sizeof arr
yields the size in bytes of the array object, not the size of a pointer; and
- When it's a string literal in an initializer used to initialize an array (sub-)object, so
char s[6] = "hello";
copies the array value into s
rather than nonsensically initializing an array object with a pointer value. This last exception doesn't apply to the code you're asking about.
(The N1570 draft of the 2011 ISO C standard incorrectly states that _Alignof
is a fourth exception; this is incorrect, since _Alignof
can only be applied to a parenthesized type name, not to a expression. The error is corrected in the final C11 standard.)
Recommended reading: Section 6 of the comp.lang.c FAQ.