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 s[i]
is equivalent to *( s + i )
that from the mathematical point of view is equivalent to *( i + s )
and in C may be rewritten like i[s]
because in any case the expression is considered like *( i + s )
.
Thus all these constructions
s[ i ]
*(s+i)
*(i+s)
i[s]
are equivalent in C and denote the i-th
element of the array s
.
As for your comment to my answer
Could you please explain how the for loop becomes true during the
initial iteration?
then the array s
is initialized by the string literal "d"
.
char s[ ] = "d";
A string literal includes into itself the terminating zero character '\0'
. So the declaration above can be in fact equivalently rewritten the following way
char s[ ] = { 'd', '\0' };
As the first element of the array is not equal to zero (it is equal to the character 'd'
) then the condition s[i]
that is equivalent to the condition s[i] != 0
is true when i
is equal to 0
.
And a quote regarding to the for-statement (6.8.5 Iteration statements)
4 An iteration statement causes a statement called the loop body to
be executed repeatedly until the controlling expression compares equal
to 0. The repetition occurs regardless of whether the loop body is
entered from the iteration statement or by a jump.1