Why does the following expression:
total += *start++;
Evaluate to:
total += (*start)++;
And not:
total += *(start++); // though this doesn't really matter either it would be the same
The ++
(postfix increment) and *
(dereference have the same precedence and associate from right-to-left, so why isn't the ++
postfix evaluated first?
Or, does the postfix evaluate after a sequence point, and so:
total += *++start
Would evaluate to:
total += *(++start)
But because the postfix happens after:
total += *start++
Would evaluate to:
total += (*start)++;
In other words, Right-to-Left associativity is not important in the above expression, because even post-fix is evaluated after not during the expression evaluation?
The postfix ++
operator does have higher precedence than the dereference operator *
. So the expression parses as:
total += *(start++);
What might be confusing you is that the result of the postfix ++
operator is the operand before it is incremented. The actual increment happens as an unsequenced side effect of the expression.
So this expression takes the original value of start
, dereferences it, and adds that value to total
. By the time the expression is fully evaluated, start
is incremented.
Note that this differs from:
total += (*start)++;
Because this increments what start
points to instead of start
itself.