By definition, in every standard of C, x[y] is equivalent to (and often compiled as) *((x)+(y)). Additionally, a name of an array is converted to an address operator to it -- so if x is an array, it would be *((&(x))+(y))
So, for a multidimension array, x as a 2 dimension array, x[y][z] would be equivalent to (((&(x))+(y))+(z))
In the small scale toy C compiler I'm working on, this fails to generate proper code, because it tries to indirectly access a pointed to address at every * instruction -- this works for single dimension arrays, but for multi dimension it results in something like (in vaguely assembly pseudocode)
load &x; add y; deref; add z; deref
Where deref is an instruction to load the value at the address of the previous calculation -- as this is how the indirection operator seems to work??
However, this will generate bad code, since we should be dealing all with a single address, only dereferencing at the very end. I'm assuming there's something in the spec I'm missing?
x
is converted to&x[0]
, which has different type compared to&x
.deref
does depends on the type, and you have to detect that. Generally, yes,deref() { if simple pointer; then deref; if array; then only remove one dimension from type and don't change the value, if pointer to function, then do nothing }
&*
is a no-op, so you have to check if the next operation is&
and then do nothing, for example.