-4

I've came across this interesting snippet. I said no way it could work (without crashing). Shockingly, it runs "ok" both on Linux (compiled with gcc) and on OSX (llvm). Even more disturbingly, Valgrind doesn't find anything wrong!!!

What gives?

int main(void) {
  int a[3];
  2[a] = 1;
}

1 Answers1

5

In short and simple, the compiler translates anything in the form x[y] to *(x + y). So 2[a] translates to *(2 + a) which is the same as *(a + 2) from a[2].

Eregrith
  • 4,263
  • 18
  • 39