C weird array syntax in multi-dimensional arrays

风格不统一 提交于 2019-12-18 13:09:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!