I am reading through the section about pointers and arrays from "The c++ programming language", and I came across something that stumps me.
For every built-in array
a
and integerj
within the range ofa
, we have:
a[j] == *(&a[0]+j) == *(a+j) == *(j+a) == j[a]
I understand why *(&a[0]+j)
means the same as a[j]
, because &a[0]
points to the first element of a
and then it's incremented by j
.
I also get that *(a+j)
and *(j+a)
are equal to a[j]
, a gets implicitly converted from an array to a pointer to the first element of a, and then incremented by j and dereferenced.
But why is a[j] == j[a]
?
What am I missing?
Because the subscript operator is commutative. It is simply specified to be so by the language.
You can see the symmetry from the equivalence with the pointer arithmetic expressions, where the addition operator is also commutative:
The language standard (draft) says: