In With arrays, why is it the case that a[5] == 5[a]? it is explained that the []
operator in a[5]
is defined as *(a + 5)
and because +
is commutative, 5[a]
means *(5 + a)
and so the two expressions refer to the same memory location. Fine.
However, C also defines in 6.4.2.1 that an identifier cannot start with a digit. In 5[a]
the array identifier is 5
which is not a valid identifier. Why does 5[a]
not generate an error about an invalid identifier?
5
is not an identifier, it is an integer literal.
The C standard literally state that 5[a]
is just syntactic sugar that must be equivalent with *(5 + a)
. There is no requirement in C that the first operand of the + operator is an identifier, so the code works just fine.
6.5.6, emphasis mine:
For addition, either both operands shall have arithmetic type, or one operand shall be a pointer to a complete object type and the other shall have integer type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With