Are a, &a, *a, a[0], &a[0] and &a[0][0] identical pointers?
Solution 1
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 typeint(*)[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 typeint(*)[2]
. -
*a
dereferences the pointer expressiona
. Sincea
(after it decays) is a pointer to an array of 2int
s,*a
is an array of 2int
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 typeint*
.*a
is equivalent to&a[0][0]
. -
&a[0]
is the address of the first (0th) row of the array object. It's of typeint(*)[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 typeint*
.
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\n", (void*)&a);
printf("a = %p\n", (void*)a);
printf("*a = %p\n", (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
, sosizeof 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 intos
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.
Solution 2
Because all expressions are pointing to the beginning of the array:
a = {{a00},{a01},{a10},{a11}}
a
points to the array, just because it is an array, so a == &a[0]
and &a[0][0]
is positioned at the first cell of the 2D array.
Solution 3
+------------------------------+
| a[0][0] <-- a[0] <-- a | // <--&a, a,*a, &a[0],&a[0][0]
|_a[0][1]_ |
| a[1][0] <-- a[1] |
| a[1][1] |
+------------------------------+
Solution 4
It is printing out the same values because they all are pointing to the same location.
Having said that,
&a[i][i]
is of type int *
which is a pointer to an integer.
a
and &a[0]
have the type int(*)[2]
which indicates a pointer to an array of 2
ints.
&a
has the type of int(*)[2][2]
which indicates a pointer to a 2-D array
or a pointer to an array of two elements in which each element is an array of 2-ints.
So, all of them are of different type and behave differently if you start doing pointer arithmetic on them.
(&a[0][1] + 1)
points to the next integer element in the 2-D array i.e. to a[0][1]
&a[0] + 1
points to the next array of integers i.e. to a[1][0]
&a + 1
points to the next 2-D array which is non-existent in this case, but would be a[2][0]
if present.
Solution 5
A 2D array in C is treated as a 1D array whose elements are 1D arrays (the rows).
For example, a 4x3 array of T
(where "T" is some data type) may
be declared by: T a[4][3]
, and described by the following
scheme:
+-----+-----+-----+
a == a[0] ---> | a00 | a01 | a02 |
+-----+-----+-----+
+-----+-----+-----+
a[1] ---> | a10 | a11 | a12 |
+-----+-----+-----+
+-----+-----+-----+
a[2] ---> | a20 | a21 | a22 |
+-----+-----+-----+
+-----+-----+-----+
a[3] ---> | a30 | a31 | a32 |
+-----+-----+-----+
Also the array elements are stored in memory row after row.
Prepending the T
and appending the [3]
to a
we have an array of 3
elements of type T
. But, the name a[4]
is itself an array indicating that there are 4
elements each being an array of 3
elements. Hence we have an array of 4
arrays of 3
elements each.
Now it is clear that a
points to the first element (a[0]
) of a[4]
. On the Other hand &a[0]
will give the address of first element (a[0]
) of a[4]
and &a[0][0]
will give the address of 0th
row (a00 | a01 | a02)
of array a[4][3]
. &a
will give the address of 2D array a[3][4]
. *a
decays to pointers to a[0][0]
.
Note that a
is not a pointer to a[0][0]
; instead it is a pointer to a[0]
.
Hence
- G1:
a
and&a[0]
are equivalent.- G2:
*a
,a[0]
and&a[0][0]
are equivalent.- G3:
&a
(gives the address of 2D arraya[3][4]
).
But group G1, G2 and G3 are not identical although they are giving the same result (and I explained above why it is giving same result).
Comments
-
Koderok almost 2 years
I have the following C program:
#include <stdio.h> int main(){ int a[2][2] = {1, 2, 3, 4}; printf("a:%p, &a:%p, *a:%p \n", a, &a, *a); printf("a[0]:%p, &a[0]:%p \n", a[0], &a[0]); printf("&a[0][0]:%p \n", &a[0][0]); return 0; }
It gives the following output:
a:0028FEAC, &a:0028FEAC, *a:0028FEAC a[0]:0028FEAC, &a[0]:0028FEAC &a[0][0]:0028FEAC
I am not able to understand why are
&a
,a
,*a
- all identical. The same fora[0]
,&a[0]
and&a[0][0]
.EDIT:
Thanks to the answers, I've understood the reason why these values are coming out to be equal. This line from the book by Kernighan & Ritchie turned out to be the key to my question:
the name of an array is a synonym for the location of the initial element.
So, by this, we get
a
=&a[0]
, anda[0]
=&a[0][0]
(consideringa
as an array of arrays)Intuitively, now the reason is clear behind the output. But, considering how pointers are implemented in C, I can't understand how
a
and&a
are equal. I am assuming that there is a variablea
in memory which points to the array(and the starting address of this array-memory-block would be the value of this variablea
).But, when we do
&a
, doesn't that mean taking the address of the memory location where the variablea
was stored? Why are these values equal then? -
Jonathan Leffler over 10 years+1 for pointing out that they are three different types which happen to have the same value.
-
Koderok over 10 yearsSorry, the picture is still not clear. Are you using
mat
anda
interchangeably? You saidmat
is a pointer tomat[0]
, but in the figure, you have shownmat == mat[0]
(doesn't this mean they are equal?). Also, this doesn't seem quite right: "mat[0][0] will give the address of 0th row of array mat[4][3]". Doesn'tmat[0][0]
give the contents rather than an address? -
Koderok over 10 yearsok, thanks! Could you please also answer my other queries in the previous comment.
-
haccks over 10 yearsYes. You are right
mat[0][0]
will give content not the address. It was typing error. My bad. -
Timothy Shields over 10 yearsYou were right the first time -
a[X]
is equal to*(a + X)
. The pointer arithmetic already takes into account the size of the type it points to. -
haccks over 10 yearsIf you think 2D array as 1D i.e
a[3][4]
asa[4]
: having 4 elements each of which are an array of 3 elements thena
will point to the first element of the arraya[4]
. You can say that now thata
,&a
and&a[0]
are identical but are different froma[0]
and&a[0][0]
.a[0]
is identical toa[0][0]
. -
Koderok over 10 yearsThanks! It's pretty clear. Only one thing that I need to understand now - for any general array
a
(say, 1-D array), why area
and&a
identical? -
Koderok over 10 yearsIsn't
a
of typeint **
? Dereferencinga
by*a
would print the value ofa[0]
, which is of the typeint *
. On the other hand, dereferencing it twice using**a
would print the value ofa[0][0]
(an integer). -
Keith Thompson over 10 yearsThe object
a
is of typeint[2][2]
, which is an array type, not a pointer type. The expressiona
, unless it's the operand of unary&
orsizeof
, is, after the implicit conversion, of typeint(*)[2]
, or pointer to 2-element array ofint
.a
, after conversion, is equivalent to&a[0]
(or&(a[0])
if that's clearer). @PulkitYadav: No, it's not of typeint**
; there is noint*
object for it to point to. -
haccks over 10 yearsAlways remember that Each variable in the program occupies one or more bytes of memory; the address of the first byte is said to be the address of the variable and also the name of the array is a pointer to the first element.
a
is pointer to first element [a[0]
) while&a
is pointer to arraya[]
and since address of the first byte (a[0]
) is address of variablea[]
, both seems to be identical but they are not identical! -
haccks over 10 years@KeithThompson; I think
a
has typeint (*) [2]
(pointer to an integer array of length 2), isn't it? -
Keith Thompson over 10 years@haccks: Yes, that's what I wrote. I know I edited my comment, but I don't remember exactly how; were you responding to an earlier version that was less clear, or is it still unclear?
-
haccks over 10 years@KeithThompson; Still unclear. I think type
int (*)[2]
is different from array type. Am I wrong? -
Keith Thompson over 10 years@haccks:
int (*)[2]
is a pointer type, so yes, it's different from the array typeint[2][2]
. The objecta
is of typeint[2][2]
. The expressiona
is implicitly converted, in most but not all contexts, to a pointer to the array object's first element, yielding an expression of typeint(*)[2]
. Butsizeof a
, for example, is the same assizeof (int[2][2])
. -
haccks over 10 years@KeithThompson; Got it. In place of
a
has typeint (*) [2]
, it should be when used as a pointer,a
has typeint (*) [2]
. Am I right? -
Keith Thompson over 10 years
-
Keith Thompson over 10 years(Hit the chat button accidentally.) Rather than "when used as a pointer", I'd say "when used in any context that converts it to a pointer". (I suppose "used as a pointer" is implied by that.)
-
haccks over 10 yearsI read that
a[i]
gives the address ofith
row. Now I am thinking that&a[i]
is identical toa[i]
. Isn't it? -
Keith Thompson over 10 years@haccks:
a[i]
is an array object of typeint[2]
, which is rowi
ofa
. In most contexts the expressiona[i]
decays to a pointer to the first element of that array, i.e., a pointer to theint
objecta[i][0]
. But in&a[i]
, sincea[i]
is the operand of unary&
, it doesn't decay, and&a[i]
is the address of the array objecta[i]
, and is of typeint(*)[2]
. No,&a[i]
anda[i]
are not identical; the former is the address of rowi
ofa
, anda[i]
is either that row or the address of the first element of that row, depending on the context. -
newacct over 10 years"
&a[0][0]
is the same as&(*(a + 0 + 0))
" No. They don't even have the same type.&a[0][0]
is the same as&(*(*(a + 0) + 0)))
-
nouney over 10 years@newacct I was not sure about it. At first I've written
&(*(*(a + 0) + 0)))
, but I don't know how to prove that&(*(*(a + 0) + 0)))
=a
actually ... How can you do that ? -
newacct over 10 years@nouney: Well, they are not the same, because they have different types. However, their addresses are the same. First of all,
a
(an array expression) is implicitly converted to&a[0]
in most contexts, so that's why the first one is true.&a[0]
has the same address value as&a[0][0]
, becausea[0]
is an array, and it is obvious from how arrays work in C that this must be true. (Note: ifa[0]
were a pointer this would not be true. So the fact that it's an array is important.) -
nouney over 10 years@newacct Oh yes I know they have different types, I just wanted to prove in a "mathematical way" (which seems impossible) that the address of both was the same :) Thanks for the explanation !
-
torek over 10 yearsI made a diagram many years ago of the difference between these three pointers (or ones similar enough), which is here. Another way to look at this is that they point to the same base address, but to a different "size unit" at that base-address.
-
haccks over 10 yearsOK. Let's continue this again. I have two questions: 1. What do you mean by
a[i]
is either that row or the address of the first element of that row, depending on the context? 2. You saida
is equivalent to&a[0]
and*a
is equivalent to&a[0][0]
, can I replace the word equivalent by identical? -
Keith Thompson over 10 years@haccks:
a[i]
is an expression of array type (specifically, it's of typeint[2]
). Ifa[i]
is the operand of unary&
orsizeof
, it refers to that array object; in any other context, it's implicitly converted to the address of the first element of that array object. 2. I suppose so; they're expressions of the same type and value, but they're written differently. I think "equivalent" works a little better. Is(2+2)
identical to4
? -
Keith Thompson about 10 years@haccks: I see the smiley, but I don't get the joke. Why should I delete that answer, and why did you post here rather than there?