Yesterday, I found this funny little piece of code:
#include <iostream>
int main()
{
int a[2] = {1, 2};
std::cout << 1[a];
return 0;
}
And it runs, printing out the 2 to the console. I don't get why. My only idea that since a[1]
is equal to *(a + 1)
, in theory we could swap the operations, so we would get *(1+a)=1[a]
. But accepting the []
operator for a number still looks strange. Does anyone have an official answer to this question?