How does that compile?

1
2
3
4
int main(){
   int i = 1, j = 2;
   return i[&j];
}

No errors, no warnings. I get 1.
What am I missing?
The standard C++ says that one of the operands of [] operator has to be a pointer and the other has to be an integer.
Since i is an integer and &j is a pointer it would compile fine.
Doing that may cause segmentation fault or unexpected results at runtime but is syntactically correct
because :

pointer_to_something [ index_value] is the same as index_value [pointer_to_something]

In this case because the values are on the stack, the two integers are next to each other - in this case i is higher in memory than j.
So i is next along in memory from j - so &j[1] is i (butnot the other way around|)


EDIT:
Special Note:
I believe the order in which local variables are stored on the stack is compiler dependent
Last edited on
Neat. I had no idea..
But then it's a little weird why operator [] can only be overloaded as a member function..
Topic archived. No new replies allowed.