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?
a[b]
is defined to be equivalent to*(a + b)
, and+
commutes. – melpomene Jun 1 '19 at 23:37a[j]
is syntactic sugar for*(a+j)
.j[a]
is syntactic sugar for*(j+a)
. And*(j+a)
is the same as*(a+j)
. – Eljay Jun 1 '19 at 23:41