Confusing answers : One says *myptr++ increments pointer first,other says *p++ dereferences old pointer value

我怕爱的太早我们不能终老 提交于 2019-12-13 21:19:20

问题


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

1) What is the difference between *myptr++ and *(myptr++) in C

2) Yet another sequence point query: how does *p++ = getchar() work?

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.

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.


回答1:


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++

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.

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.




回答2:


The accepted answer for the first question,concise and easily to understand states that since ++ has higher precedence than *,

Right. That is correct.

the increment to the pointer myptr is done first and then it is dereferenced.

It doesn't say that. Precedence determines the grouping of the subexpressions, but not the order of evaluation.

That the precedence of ++ is higher than the precedence of the indirection * says that

*myptr++

is exactly the same (not on the cource code level, of course) as

*(myptr++)

and that means that the indirection is applied to the result of the

myptr++

subexpression, the old value of myptr, whereas (*myptr)++ would apply the increment operator to what myptr points to.

The result of a postfix increment is the old value of the operand, so

*myptr++ = something;

has the same effect as

*myptr = something;
myptr++;

When the side-effect of storing the incremented value of myptr happens is unspecified. It may happen before the indirection is evaluated, or after that, that is up to the compiler.




回答3:


Section 6.5.2.4 of the C specification discusses the postfix increment and decrement operators. And the second paragraph there pretty much answers your question:

The result of the postfix ++ operator is the value of the operand. As a side effect, the value of the operand object is incremented (that is, the value 1 of the appropriate type is added to it).
...
The value computation of the result is sequenced before the side effect of updating the stored value of the operand.

So given *myptr++, yes it's true the the ++ part has higher precedence; but precedence does not exclusively determine your result. The language defines that with the specs. In this case the value of myptr is returned, then the "side effect" of myptr being incremented is executed.



来源:https://stackoverflow.com/questions/16281872/confusing-answers-one-says-myptr-increments-pointer-first-other-says-p-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!