(p ++) & # 8594; x Why aren't brackets needed? (K & R)

From page 123 of the C & K Programming Language :

(p ++) -> x increments p after accessing x. (This last set of parentheses is not needed. Why?)

Why is this not necessary, given that it ->binds stronger than ++?

EDIT: Contrast this expression with ++p->x, the latter is evaluated as ++(p->x), which will increase x, not p. Therefore, in this case, brackets are necessary and we must write (++p)->xif we want to increase p.

+5
source share
5 answers

, -> , ++. ( , @KerrekSB.)

p x.

, x p, p. -> +.

: , ...

, , ++p->x, , ++(p->x), (++p)->x ( , , K & R , , ). , p++->x, (p++)->x. p(++->x), p(++->)x p++(->x) "".

+4

:

p++(->x)

. . (p++)->x.

+5

munch , p++->x :

p, ++, ->, x

p++->x : postfix ++ postifx ->. , , . p++->x (p++)->x.

++p->x .

++p->x ++ , ++. C , ++p->x ++(p->x).

EDIT: .

+3

post-increment member access . , , .

p++->x

postfix-++ (.. p).

→ x , p++. (p++) , .

"after" . , p++ p, , , .

+2

Expresion p++leads to a pointer with a value p. The ++part is later executed , but for the purpose of interpreting the expression, it also cannot be there. ->xforces the compiler to add the offset for the member xto the source address in pand access this value.

If you change the instruction to:

p->x; p++;

he will do the same.

The priority order is actually exactly the same as you can see here - but it does not really matter.

+1
source

All Articles