Let's see what is written in the standard,
here:
http://www.open-std.org/jtc1/sc22/wg21/docs/wp/html/oct97/expr.html[
^]
Please see the section
#5.2.1 Subscripting
"A postfix expression followed by an expression in square brackets is a postfix expression. One of the expressions shall have the type "pointer to T" and the other shall have enumeration or integral type. The result is an lvalue of type "T." The type "T" shall be a com-pletely-defined object type.3) The expression E1[E2] is identical (by definition) to *((E1)+(E2)). [Note: see _expr.unary_ and _expr.add_ for details of * and + and _dcl.array_ for details of arrays. ]"
So, arr[5] is the same as *(arr + 5) == *(5 + arr) == 5[arr].
Using the transitivity of addition (meaning that A+B == B+A) we can
get some pretty ugly, but valid, syntax when indexing into arrays:
So arr[i] == *(arr + i) then we apply the transitivity rule on the left hand giving *(i + arr) and then go back to the array-form gives i[arr].
So given array arr then 5[arr] will give the third element in arr.
And then we replace the 'i' with a variable and we get something like this:
int main()
{
int arr[3] = {1, 2, 3};
for (int i = 0; i < 3; ++i)
{
std::cout << i[arr];
}
return 0;
}