I read this in my book (and many sources on the internet):
The array variable points to the first element in the array.
In most circumstances, an expression of array type ("N-element array of T
") will be replaced with / converted to / "decay" to an expression of pointer type ("pointer to T
"), and the value of the expression will be the address of the first element in the array.
So, assuming the declaration
int a[10];
the type of the expression a
is "10-element array of int
", or int [10]
. However, in most contexts, the type of the expression will be converted to "pointer to int
", or int *
, and the value of the expression will be equivalent to &a[0]
.
The exceptions to this rule are when the array expression is the operand of the sizeof
or unary &
operators, or is a string literal being used to initialize another array in a declaration.
So, based on our declaration above, all of the following are true:
Expression Type Decays to Value ---------- ---- --------- ----- a int [10] int * address of the first element of a &a int (*)[10] n/a address of the array, which is the same as the address of the first element &a[0] int * n/a address of the first element of a *a int n/a value of a[0] sizeof a size_t n/a number of bytes in the array (10 * sizeof (int)) sizeof &a size_t n/a number of bytes in a pointer to an array of int sizeof *a size_t n/a number of bytes in an int sizeof &a[0] size_t n/a number of bytes in a pointer to int
Note that the expressions a
, &a
, and &a[0]
all have the same value (address of the first element of a
), but the types are different. Types matter. Assume the following:
int a[10];
int *p = a;
int (*pa)[10] = &a;
Both p
and pa
point to the first element of a
, which we'll assume is at address 0x8000
. After executing the lines
p++;
pa++;
however, p
points to the next integer (0x8004
, assuming 4-byte int
s), while pa
points to the next 10-element array of integers; that is, the first integer after the last element of a
(0x8028
).