令人困惑的答案:一个说* myptr ++首先增加指针,另一个说* p ++取消引用旧的指针值 [英] Confusing answers : One says *myptr++ increments pointer first,other says *p++ dereferences old pointer value

查看:125
本文介绍了令人困惑的答案:一个说* myptr ++首先增加指针,另一个说* p ++取消引用旧的指针值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您能为我澄清一下,我将不胜感激.以下是两个最近的问题,并给出了可接受的答案:

I would appreciate if you clarify this for me.Here are two recent questions with their accepted answers:

1) C中的* myptr ++和*(myptr ++)有什么区别

2) 另一个序列点查询:* p ++ = getchar()如何工作?

第一个问题的公认答案,简洁明了,因为++的优先级高于*,因此先完成对指针myptr的递增,然后再取消引用.我什至检查了在编译器上进行验证并对其进行验证.

The accepted answer for the first question,concise and easily to understand states that since ++ has higher precedence than *, the increment to the pointer myptr is done first and then it is dereferenced.I even checked that out on the compiler and verified it.

但是,几分钟前发布的第二个问题的可接受答案使我感到困惑.

But the accepted answer to the second question posted minutes before has left me confused.

它明确表示在*p++中严格地取消引用p的旧地址.我没有理由质疑第二个问题的最高答案的正确性,但坦率地说,我认为它与用户H2CO3的第一个问题的答案相抵触,所以任何人都可以用简单易懂的英语解释第二个问题的答案是什么意思以及*p++为何在第二个问题中取消引用p的旧值.p是否不应该先递增,因为++具有更高的优先级?实际上,如何在谢谢.

It says in clear terms that in *p++ strictly the old address of p is dereferenced. I have little reason to question the correctness of a top-rated answer of the second question, but frankly I feel it contradicts the first question's answer by user H2CO3.So can anyone explain in plain and simple English what the second question's answer mean and how come *p++ dereferences the old value of p in the second question.Isn't p supposed to be incremented first as ++ has higher precedence?How on earth can the older address be dereferenced in *p++Thanks.

推荐答案

后缀增量运算符的确具有

The postfix increment operator does have higher precedence than the dereference operator, but postfix increment on a variable returns the value of that variable prior to incrementing.

*myptr++

因此,增量操作具有更高的优先级,但是对增量返回的值(myptr的先前值)进行解引用.

Thus the increment operation has higher precedence, but the dereferencing is done on the value returned by the increment, which is the previous value of myptr.

您链接到的第一个问题的答案没有错,他在回答另一个问题.

The answer in the first question you've linked to is not wrong, he's answering a different question.

*myptr++*(myptr++)之间没有区别,因为在这两种情况下,都先进行增量,然后再取消引用以前的myptr值.

There is no difference between *myptr++ and *(myptr++) because in both cases the increment is done first, and then the previous value of myptr is dereferenced.

这篇关于令人困惑的答案:一个说* myptr ++首先增加指针,另一个说* p ++取消引用旧的指针值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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