在C的指针操作符precedence [英] operator precedence in c with pointers

查看:169
本文介绍了在C的指针操作符precedence的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何分析precedence在以下情况。

How to analyse the precedence in following situation .

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

如何以下code与上述不同。

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);
}

我期待相同的输出,但输出为* ARR值不同。

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

推荐答案

Postfix的运营商有更高的precedence比一元运算符,所以 * X ++ 被解析为 *(X ++);恩pression 的结果X ++ (这是 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.

* + X ,无论 * ++ <的情况下, / code>是一元运算符,因而具有相同的precedence,所以运营商应用左到右,或 *(++ x) ;恩pression 的结果++ X (这是 X + sizeof的* X )将被取消引用。

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.

这篇关于在C的指针操作符precedence的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆