问题
I've known that this is true:
x[4] == 4[x]
What is the equivalent for multi-dimensional arrays? Is the following true?
x[4][3] == 3[x[4]] == 3[4[x]]
回答1:
x[y]
is defined as *(x + (y))
x[y][z]
would become *(*(x + (y)) + z)
x[y[z]]
would become *(x + (*(y + (z))))
x[4][3]
would become *(*(x + (4)) + 3)
would become
*(*(x + 4) + 3)
3[x[4]]
would become *(3 + (*(x + (4))))
would become *(*(x + 4) + 3)
3[4[x]]
would become *(3 + (*(4 + (x))))
would become *(*(x + 4) + 3)
Which means they are all equivalent.
回答2:
Yes. In each case x
is an array which decays to a pointer and then has pointer arithmetic performed on it.
来源:https://stackoverflow.com/questions/8109261/c-weird-array-syntax-in-multi-dimensional-arrays