(p++)->x Why are the parentheses unnecessary? (K&R)

c

Solution

Exactly because `->` binds stronger than `++`. (it doesn't, thanks @KerrekSB.)

increments p after accessing x.

So first you access `x` of `p`, then you increment `p`. That perfectly matches the order of evaluation of the `->` and the `+` operators.

Edit: aww, these edit's...

So what happens when you write `++p->x` is that it could be interpreted either as `++(p->x)` or as `(++p)->x` (which one is actually chosen is just a matter of language design, K&R thought it would be a good idea to make it evaluate as in the first case). The thing is that this ambiguity doesn't exist in the case of `p++->x`, since it can only be interpreted as `(p++)->x`. The other alternatives, `p(++->x)`, `p(++->)x` and `p++(->x)` are really just syntactically malformed "expressions".

Problem

From page 123 of The C Programming Language by K&R: (p++)->x increments p after accessing x. (This last set of parentheses is unnecessary. Why?) Why is it unnecessary considering that `->` binds stronger than `++`? EDIT: Contrast the given expression with `++p->x`, the latter is evaluated as `++(p->x)` which would increment `x`, not `p`. So in this case parentheses are necessary and we must write `(++p)->x` if we want to increment `p`.

Original source