array[1]
means, by definition in the C standard, *(array+1)
. So, if array
is a pointer, this expression adds one element to the pointer, then uses the result to access the pointed-to object.
When a
is an array, you may be used to thinking of a[0]
, a[1]
, a[2]
, and so on as elements of the array. But they actually go through the same process as with the pointer above, with one extra step. When the compiler sees a[1]
and a
is an array, the compiler first converts the array into a pointer to its first element. This is a rule in the C standard. So a[1]
is actually (&a[0])[1]
. Then the definition above applies: (&a[0])[1]
is *(&a[0] + 1)
, so it means “Take the address of a[0], add one element, and access the object the result points to.”
Thus, a[1]
in the calling code and array[1]
in the called code have the same result, even though one starts with an array and the other uses a pointer. Both use the address of the first element of the array, add one element, and access the object at the resulting address.