The idea is test[3]
is actually considered as *(test+3)
which is same as *(3+test)
because of around +
the 3
and test
commute. As a result 3[test]
is also possible. It doesn't matter if you write it *(3+test)
or *(test+3)
or test[3]
and 3[test]
.
From 6.5.2.1p2 C11 standard
A postfix expression followed by an expression in square brackets [] is a subscripted designation of an element of an array object. The definition of the subscript operator []
is that E1[E2]
is identical to (*((E1)+(E2)))
. Because of the conversion rules that apply to the binary + operator, if E1 is an array object (equivalently, a pointer to the initial element of an array object) and E2 is an integer, E1[E2] designates the E2-th element of E1 (counting from zero).
E1[E2]
is indeed *((E1)+(E2))
and we know binary addition is commutative. So we can say that it is same as *((E2)+(E1))
so can't we write it as E2[E1]
. Yes we can and that's legal.