I came across a piece of code that reads:
++myStruct->counter
I'm confused on how the ++ operator and -> operator are evaluated here. The ++ has precedence over the -> operator and left to right evaluation. It appears the ++ operator would actually perform pointer arithmetic on 'myStruct', not increment the counter member.
The ++ has precedence over the -> operator and left to right evaluation.
This is not correct - postfix operators like ->
have higher precedence than unary (prefix) operators ++
and --
. The expression is parsed as
++(myStruct->counter)
so the counter
member of myStruct
is being incremented.
The postfix increment and decrement have the same precedence as the ->
operator and left-to-right associativity, but the prefix increment and decrement are after. So the code does increment the variable counter
and not the myStruct
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With