The [braket] operator is just a simplified/automated way to do pointer math.
a[0] is exactly the same thing as *a. Both simply dereference the 'a' pointer... they just use different operators to do so. By extension:
a[1] is the same as *(a+1). The latter simply adds 1 to the pointer before dereferencing. Therefore:
a[i[i][/i]] is the same as *(a + i). This is also the same as *(i + a) for the same reason 1+4 == 4+1
i[a], however, is not valid syntax (not unles 'i' is a pointer and 'a' is an integer, which in your above example, neither of those are the case). Your compiler should be throwing errors for you on line 17 of that code you pasted.
is totally valid syntax. Why? Because Kernighan or Ritchie thought it would be cool to invert the array and index, I guess. Not that it has any practical use beyond that of obfuscated programming.
I stand corrected. I guess logically (to me) there's no reason why it should work (it's like trying to dereference an integer). But I guess if there's some weird C++ rule that says it's okay.... Personally I find that ridiculous.
Though I guess if you analyze the logic...
1 2 3
*(a + i) == a[i] // if this is true
*(a + i) == *(i + a) // and this is true
*(i + a) == i[a] // then I guess by extension this is true?
Apologies for my inaccurate statement. Though this still seems absurd to me.
EDIT -
hah -- apparently while I was typing Bazzy linked to a thread which explains it the exact same way. Haw.
I guess the thing that trips me up about it is that in C++, brackets are an overloadable operator which act on the object outside of them. So i[a]would* throw a compiler error if 'a' is an object with [] overloaded.
*disclaimer: didn't actually test that -- not on my home computer and don't have easy access to a compiler right now. If that does work, though, I really would be dumbfounded.
I was wondering that myself over dinner tonight, especially if the overloaded operator[] does not take an int. Have to try it out on my linux box later.