Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

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.

like image 389
Roland Avatar asked Feb 18 '13 21:02

Roland


1 Answers

The only other possible interpretation is:

p++(->x)

and that doesn't mean anything. It's not even valid. The only possible way to interpret this in a valid way is (p++)->x.

like image 62
Nikos C. Avatar answered Sep 28 '22 08:09

Nikos C.