Why is 2 [myArray] a valid C syntax?

Duplicate

In C arrays, why is this so? a [5] == 5 [a]


Given an array

 myArray[5] = { 0, 1, 2, 3, 4 };

element can be accessed as

 2[myArray]

Why? When I see this expression, I assume that C is trying to access the "2" pointer and does not add the "myArray" pointer increment to dereference this address. What am I missing?

+5
source share
1 answer

in C, a [b] is equivalent to * (a + b). And, of course, the operator + is commutative, therefore a [b] is the same as b [a] coincides with * (b + a), coincides with * (a + b).

+17
source

All Articles