“ x&quot”是否有任何区别&&quot” x [0]&quot&quot在C中?
浏览了用C编写的一些软件,我遇到了以下代码,我可以总结一下:
void fill_array_with_some_data(int *_)
{
// assign some arbitrary int values to _[0] and _[1]
_[0] = 42;
_[1] = 24;
}
int main(int argc, char **argv)
{
int x[2] = {0};
fill_array_with_some_data(&x[0]);
// do something with the data which is now is x
if (x[0] & 0x42)
// do something
return 0;
}
查看& x [0]
,而不是简单地x
对我来说似乎很奇怪。
两者之间有任何区别吗?
有什么原因为什么要使用& x [0]
而不是简单的x
?
Going through some piece of software written in C, I encountered the following bit of code, which I can summarize as such:
void fill_array_with_some_data(int *_)
{
// assign some arbitrary int values to _[0] and _[1]
_[0] = 42;
_[1] = 24;
}
int main(int argc, char **argv)
{
int x[2] = {0};
fill_array_with_some_data(&x[0]);
// do something with the data which is now is x
if (x[0] & 0x42)
// do something
return 0;
}
Seeing &x[0]
instead of simply x
seemed odd to me.
Is there any difference between both?
Is there any reason why one would want to use &x[0]
rather than simply x
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了样式以外,没有其他区别。
传递
& x [0]
作为参数可能表明程序员只希望该函数访问第一个元素,而传递x
将暗示他们希望该功能访问该功能整个数组。但这仅仅是常规的,问题中给出的代码示例显然并不尊重这一约定。就语言标准而言,至少在评估的上下文中(除
sizeof
>):& x [0]
是用于&*(x + 0)
,可以简化为&*x
,然后x
。There aren’t any differences other than style.
Passing
&x[0]
as an argument may suggest that the programmer only wants the function to access the first element, where passingx
would imply they expect the function to access the whole array. But that would be merely conventional, and the code sample given in the question clearly does not even respect this convention.As far as the language standard is concerned, the expressions are exactly equivalent, at least in evaluated contexts (other than
sizeof
):&x[0]
is a shorthand for&*(x + 0)
, which can be simplified to&*x
and thenx
.在
中,fill_array_with_some_data(& x [0])
,x
和& x [0]
是等效的。int x [2] = {0];
,x
指定数组。当在表达式中使用数组时,它将转换为指针转换为第一个元素,除非:当sizeof x
,&
,如& x
,或者char s [] =“ ABC”;
。因此,
sizeof x
将给出数组的大小,而sizeof& x [0]
将给出指针的大小,指向x 。
& x
和& x [0]
都指向内存中的同一位置,但它们具有不同的类型。x
具有“指向2int
的数组的指针,& x [0]
具有“指针”指向int
int 代码>”。这意味着& x + 1
将指向整个数组结束后x
,但& x [0] + 1
将指向元素x [0]
之后(因此指向x [1]
)。通常,只有在一个人想要专门指向元素时,才会写
& x [0]
。在用一些数据填充数组时,程序员应编写fill_array_with_some_data(x)
传达他们期望例程在整个数组上运行。这对编译器没有影响,但它可以帮助人类了解正在做什么。人们应该使用& x [0]
特别尤其召集该元素。例如,如果例程foo
仅在一个元素上操作,则我们可以编写foo(& x [0])
向阅读器指示这一点。In
fill_array_with_some_data(&x[0])
,x
and&x[0]
are equivalent.After
int x[2] = {0];
,x
designates an array. When an array is used in an expression, it is converted to a pointer to its first element except when:sizeof x
,&
, as in&x
, orchar s[] = "abc";
.Thus
sizeof x
will give the size of the array, whereassizeof &x[0]
will give the size of a pointer to an element ofx
.&x
and&x[0]
will both point to the same location in memory, but they have different types.x
has type “pointer to array of 2int
, and&x[0]
has type “pointer toint
”. This means&x + 1
will point to after the end of the entire arrayx
, but&x[0] + 1
will point to after the elementx[0]
(so it points tox[1]
).Generally, one would write
&x[0]
only when one wants to point specifically to the element. When filling the array with some data, programmers should writefill_array_with_some_data(x)
to convey they expect the routine to operate on the entire array. This makes no difference to the compiler, but it helps humans understand what is being done. One should use&x[0]
only to call out that element particularly. For example, if a routinefoo
operates on only one element, we might writefoo(&x[0])
to indicate that to the reader.