1

Possible Duplicate:
In C arrays why is this true? a[5] == 5[a]

Consider the following code able to compile under VC2008.

int i = 0;

int *j = 0;
int k = 0;

i[j];    // OK?!?!
i[k];    // Compile Error.

I was wondering, what is the meaning of i[j] in this content?

Community
  • 1
  • 1
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
  • 1
    In effect, if not in actual expression a possible duplicate of [In C arrays why is this true? a\[5\] == 5\[a\]](http://stackoverflow.com/questions/381542/in-c-arrays-why-is-this-true-a5-5a) – CB Bailey Oct 04 '10 at 07:14

2 Answers2

4

i[j] equals to j[i]

Therefore it's doing *(j + i) which is actually valid since j is a pointer.

This doesn't apply for k because it isn't a pointer.

Luca Matteis
  • 29,161
  • 19
  • 114
  • 169
2

It is one of the shocked but legal feature of C/C++ i[j] form to mark offset relative base address and dereference it. So it is legal. But i[k] - cannot be dereferenced, that is why it is an error.

Dewfy
  • 23,277
  • 13
  • 73
  • 121