tags:

views:

75

answers:

2

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?

+4  A: 

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
+1  A: 

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