关于C中的*(星号)和++的混淆 [英] confusion regarding *(asterisk) and ++ in c

查看:145
本文介绍了关于C中的*(星号)和++的混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int main()
{
  int a=10;
  int *b=&a;
  int c=*b++;
  printf("%d",c);
}

我知道以下程序输出10.但根据优先级表,该表给出了运算符 http://www.difranco.net/compsci/C_Operator_Precedence_Table.htm ),后缀++的优先级高于=和*.因此++应该先评估然后是*.那么为什么程序将输出打印为10?

I know following program outputs 10.but according to precedence table which gives precedence of operator http://www.difranco.net/compsci/C_Operator_Precedence_Table.htm) ,post-fix ++ has higher precedence than = and *.so ++ should evaluate first and then *.then why program is printing output as 10?

推荐答案

它的优先级更高,这意味着指针将增加一,而不是其指向的值.

It does have higher precedence, which means the pointer will get increased by one, not the value it points to.

但是,加1的顺序是在求值的末尾排序(这是postfix ++的作用,并且不与priority直接相关),因此从指针中获得的值是旧值:p不是p+1.有了该指针,您就可以取消引用它并获得a的值,即10.然后,指针p会增加一个.

But the increase by one is sequenced to the end of the evaluation( that is what postfix ++ does, and that is not directly related to precedence ), so the value you get out of the pointer is the old one: p not p+1. Having that pointer, you dereference it and get the value of a, which is 10. Then the pointer p is incremented by one.

int c = *p ;
p++ ;

这篇关于关于C中的*(星号)和++的混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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