c 中指针的运算符优先级

发布于 2024-12-17 20:54:02 字数 316 浏览 1 评论 0原文

下面的情况如何分析优先级。

for (i=0; i<20; i++)
{
    *array_p++ = i*i;
    printf("%d\n",*arr++);
}

以下代码与上面有何不同。

for (int i=0; i<20; i++)
{
    *arr = i*i;
    printf("%d\n",*arr);
    arr++; 
    printf("%d\n",(int )arr);
}

我期望相同的输出,但 *arr 值的输出不同

How to analyse the precedence in following situation .

for (i=0; i<20; i++)
{
    *array_p++ = i*i;
    printf("%d\n",*arr++);
}

how is following code different from above.

for (int i=0; i<20; i++)
{
    *arr = i*i;
    printf("%d\n",*arr);
    arr++; 
    printf("%d\n",(int )arr);
}

I am expecting same output but outputs are different for *arr value

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

余生再见 2024-12-24 20:54:02

后缀运算符的优先级高于一元运算符,因此 *x++ 被解析为 *(x++);表达式x++(即x)的结果被取消引用。

*++x 的情况下,*++ 都是一元运算符,因此具有相同的优先级,因此应用运算符从左到右,或*(++x);表达式++x(即x + sizeof *x)的结果被取消引用。

Postfix operators have higher precedence than unary operators, so *x++ is parsed as *(x++); the result of the expression x++ (which is x) is dereferenced.

In the case of *++x, both * and ++ are unary operators and thus have the same precedence, so the operators are applied left-to-right, or *(++x); the result of the expression ++x (which is x + sizeof *x) is dereferenced.

相守太难 2024-12-24 20:54:02

引用 Wikipediapostfix ++ 绑定在一元 * 之前。这意味着您有 *(arr++)。例如,在表达式 *arr++ = 5 中,*arr 被赋值为 5,然后 arr 递增。

在K&R中,这个技巧被用来编写memcpy的简洁版本。就像:

while (--size)
    *dest++ = *src++;

今晚我回家后我会发布正确的例子。

编辑:显然,只有 postfix ++ 具有更高的优先级。维基百科说前缀 ++ 具有同等优先级。

Citing Wikipedia, postfix ++ binds before unary *. This means that you have *(arr++). For example, in the expression *arr++ = 5, *arr is assigned to 5, then arr is incremented.

In the K&R, this trick is used to write a concise version of memcpy. It's something like:

while (--size)
    *dest++ = *src++;

I'll post the correct example after I get home tonight.

Edit: Apparently, only postfix ++ has higher precedence. Wikipedia says prefix ++ has equal precedence.

贵在坚持 2024-12-24 20:54:02

当您考虑前缀增量时,很容易记住哪个优先。

这里什么优先?

*++数组=x; // 很明显,

后缀表达式具有相同的优先级规则,因此它相当简单。

对于强制转换运算符也是如此。

(somestruct *)x + 1;

首先完成转换,因为如果不是以下内容,则没有意义

x + (int)1;

It is easy to remember which has precedence when you consider prefix increment.

what has precedence here?

*++array = x; // pretty obvious

the same precedence rules go for the postfix expression so its rather easy.

The same goes for the cast operator.

(somestruct *)x + 1;

first the cast is done since if it wasn't the following wouldnt make sense

x + (int)1;

宫墨修音 2024-12-24 20:54:02

在第一个示例代码片段中,

    for (i=0; i<20; i++)
        {
          *array_p++ = i*i;
           printf("%d\n",*arr++);
    }

array_ptr 首先递增,即地址,然后将根据 i*i 计算的值分配给 *array_ptr,因为计算按照从右到左的顺序,即 *(array_ptr ++)。

在代码片段的第二个示例中,

for (int i=0; i<20; i++)
{
  *arr = i*i;
printf("%d\n",*arr);
 arr++; 
printf("%d\n",(int )arr);
}

首先计算从 i*i 计算出的值并将其分配给 *arr,然后递增指向 arr 的指针。

因此,两个代码片段之间的值存在差异。

In the first sample code snippet,

    for (i=0; i<20; i++)
        {
          *array_p++ = i*i;
           printf("%d\n",*arr++);
    }

array_ptr is incremented first i.e address, and then the value computed from i*i is assigned to *array_ptr since evaluation takes in the order of right to left i.e *(array_ptr ++).

In the second sample of code snippet,

for (int i=0; i<20; i++)
{
  *arr = i*i;
printf("%d\n",*arr);
 arr++; 
printf("%d\n",(int )arr);
}

value computed from i*i is first computed and assigned to *arr, then pointer pointing to arr is incremented.

Hence there is difference in values among the 2 code snippets.

梦纸 2024-12-24 20:54:02

运算符 ++ 和 * 具有相同的优先级(后缀或前缀)。但是,这种情况下的结合性是从右到左。
因此,在类似的情况下,

*ptr++ ==>  *(ptr++) 
*++ptr ==> *(++ptr)
++*ptr ==> ++(*ptr)

此链接应该为您提供有关运算符优先级及其关联性的更多信息。
http://www.isthe.com/chongo/tech/comp /c/c-precedence.html

Operators ++ and * have the same precedence (postfix or prefix).However the associativity in such cases is from right to left.
so in cases like

*ptr++ ==>  *(ptr++) 
*++ptr ==> *(++ptr)
++*ptr ==> ++(*ptr)

this link should give you more information on operators precedence and their associativity..
http://www.isthe.com/chongo/tech/comp/c/c-precedence.html

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