“ x&quot”是否有任何区别&&quot” x [0]&quot&quot在C中?

发布于 2025-01-21 19:13:56 字数 570 浏览 6 评论 0原文

浏览了用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠檬色的秋千 2025-01-28 19:13:56

除了样式以外,没有其他区别。

传递& 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 passing x 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 then x.

黎夕旧梦 2025-01-28 19:13:56

中,fill_array_with_some_data(& x [0])x& x [0]是等效的。

int x [2] = {0];x指定数组。当在表达式中使用数组时,它将转换为指针转换为第一个元素,除非:当

  • 它用作sizeof的操作数,例如sizeof x
  • 它被用作操作数Unary &,如& x,或者
  • 它是用于初始化数组的字符串,如char s [] =“ ABC”;

因此,sizeof x将给出数组的大小,而sizeof& x [0]将给出指针的大小,指向x 。

& x& x [0]都指向内存中的同一位置,但它们具有不同的类型。 x具有“指向2 int的数组的指针,& 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:

  • it is used as the operand of sizeof, as in sizeof x,
  • it is used as the operand of unary &, as in &x, or
  • it is a string literal used to initialize an array, as in char s[] = "abc";.

Thus sizeof x will give the size of the array, whereas sizeof &x[0] will give the size of a pointer to an element of x.

&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 2 int, and &x[0] has type “pointer to int”. This means &x + 1 will point to after the end of the entire array x, but &x[0] + 1 will point to after the element x[0] (so it points to x[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 write fill_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 routine foo operates on only one element, we might write foo(&x[0]) to indicate that to the reader.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文