-2

Today, I see such a wierd way of indexing the arry.

The code is like:

int array[] = {10, 20, 30};
cout << -2[array];

I've never seen such a strange way of using array. But there is no compilation error.

Can anyone tell me does the ISO document evolve the description for this way of using array?

1 Answers1

3

It works, because expressions of the form x[y] is just sugar for *(x+y), and of course, the addition is commutative, so 2[array] and array[2] get compiled to the same thing.

Don't do it though, because it's unnecessarily confusing.

amnn
  • 3,657
  • 17
  • 23