...so X[Y] just adds the two together and dereferences?? That's...(NUTS_flag & SOMEHOW_SMART_flag)!!!
(WARNING: Technical ramble follows; especially in the sense of "technicality"...)
I'd bet it's true that "int array[]"
does declare an actual block of memory (which can then not be changed as a whole, since it's declared directly than that being a
pointer to some block), and then using "array" by itself just gives it's address ...
HOWEVER, I think that the
conceptual implication is that it
IS a pointer: You use it in all the same ways with all the same syntax, and even the variable name by itself gives you the address of the array (or rather, the first element of it) that it is "pointing to". This concept must hold true in order for you to be able to assign this kind of array to a pointer (e.g. to an "int*" rather than an "int[]*", if such a thing existed). The only difference is that you cannot reassign it. Of course, the
actuality (which is important to understand) is that the compiler stores it statically, but then adds in automatic addressing and stuff so that it behaves
as if it it were a pointer to an underlying array. (For this reason, I am surprised that "int array[]" is not just MADE constant elsewhere just to be uniform with what that declaration is "supposed" to mean [as I explained it, anyway; e.g. maintaining the
conceptual implications of being const rather than the actuality that it
has to be a pointer when it's a parameter for it to even work uniformly] ... though I suppose it also doesn't surprise me, since C++ tends to exploit actualities).
...Anyway............... that all aside, if we treat "int* x" and "int y[]" both as int-pointers (or int-array pointers), then it makes sense (in the easy-compiler-coding sense) to just have "X[Y]" mean "*(X+Y)" since "x[i]" means "*(x+i)" and "y[i]" means "*(y+i)"; which in turn makes the initially bizarre "2[x]" syntax suddenly make perfect sense! ... AHHH!!!!!!
On a side note, these ties between arrays and deferencing have brought me to consider coming up with my own syntax and semantics for such:
- A minimalist approach would abolish "[]" and resort to things like *(base+index)" ... you can do all the same stuff that way anyway
- If "X[Y]" and "Y[X]" are identical to "*(X+Y)", then perhaps both could be replaced with a syntax like "[X+Y]" (which is just my minimalist approach all over again, but using "[X]" instead of "*X"). A point in favor of this is that it always makes it clear what order things happen in, versus "*a[i]" (e.g. "[[a+i]]" or "[[a]+i]").