OK, I wrote the following code to test this:
int main(){
int a[10];
a[9] = 1;
9[a] = 1;
}
ran : gcc -S test.c
and this is the contents of test.s:
.file "test.c"
.version "01.01"
gcc2_compiled.:
.text
.align 4
.globl main
.type main,@function
main:
pushl %ebp
movl %esp, %ebp
subl $56, %esp
movl $1, -20(%ebp)
movl $1, -20(%ebp)
leave
ret
.Lfe1:
.size main,.Lfe1-main
.ident "GCC: (GNU) 2.96 20000724 (experimental)"
look at the 2 movl lines, these are exactly the same and correspond to the a[9] and 9[a] statements, which proves that the statements are exactly equivalent. If this is not in the specifications of the langugage, it could be compiler dependent, but I doubt it.
I've had a programming languages class where I've designed a language, and the a are probably just an indication to add a nd b together to get a new address. The compiler does not check what kind of data type a is, or it would generate an error. Anyway, since it just adds a and b
we know that a+b == b+a
therefore, the statements a and b[a] are equivalent when b is a number and a is a variable!
hth As always, I hope that helped!
Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.