访问数组内容:x [n] vs n [x] [英] Accessing array contents: x[n] vs n[x]
问题描述
可能重复:
通过C和C ++中的索引[array]访问数组
我最近发现了一些我从来没有见过的在C(和C + +)中访问数组的内容,你可以使用 x [n]
或 n [x]
与 3 [x]
相同所以我的第一个问题是这个工作,有什么使用第二个语法是有用的(除了让我困惑)。
x [n]
为 *(x + n)
p>
n [x]
是 *(n + x)
,所以表达式是等效的。使用第二个没有什么好处。
Possible Duplicate:
Accessing arrays by index[array] in C and C++
I have recently found out something that I had never seen before in C (and C++) in that for accessing the contents of an array you can use x[n]
or n[x]
So for example x[3]
is the same as 3[x]
So my first question is how does this work and is there anything using the 2nd syntax that is useful (apart from confusing me)?
x[n]
is *(x + n)
and
n[x]
is *(n + x)
so the expressions are equivalent. There is nothing beneficial in using the second.
这篇关于访问数组内容:x [n] vs n [x]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!