According to the C Standard (6.5.2.1 Array subscripting)
2 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).
So this expression x[i]
is calculated like *( x + i )
that is the same as *( i + x )
and as i[x]
.
Pay attention to that the loop can be also rewritten the following way
int x[]={1,2,3,4,5};
const size_t N = sizeof( x ) / sizeof( *x );
for ( size_t i = 0; i < N; )
printf( "%d\n", i++[x] );
Here is a demonstrative program.
#include <stdio.h>
int main(void)
{
int x[] = { 1, 2, 3, 4, 5 };
const size_t N = sizeof( x ) / sizeof( *x );
for ( size_t i = 0; i < N; )
printf( "%d\n", i++[x] );
return 0;
}
Its output is
1
2
3
4
5