My question is about the following line of code, taken from "The C Programming Language" 2nd Edition:
*p++->str;
The book says that this line of code increments p after accessing whatever str points to.
My understanding is as follows:
Precedence and associativity say that the order in which the operators will be evaluated is
- ->
- ++
- *
The postfix increment operator ++ yields a value (i.e. value of its operand), and has the side effect of incrementing this operand before the next sequence point (i.e. the following ;)
Precedence and associativity describe the order in which operators are evaluated and not the order in which the operands of the operators are evaluated.
My Question:
My question is around the evaluation of the highest precedence operator (->) in this expression. I believe that to evaluate this operator means to evaluate both of the operands, and then apply the operator.
From the perspective of the -> operator, is the left operand p or p++? I understand that both return the same value.
However, if the first option is correct, I would ask "how is it possible for the evaluation of the -> operator to ignore the presence of the ++".
If the second option is correct, I would ask "doesn't the evaluation of -> in this case then require the evaluation of a lower precedence operator ++ here (and the evaluation of ++ completes before that of ->)"?
->
isp++
, and the reason their relative precedence doesn’t matter is that++
is a unary operator. (But order of evaluation doesn’t follow precedence anyway.) – Sympathize++
increment occurs before the next 'sequence point'. There isn't a sequence point within the expression*p++->string
, so the increment occurs before the next sequence point in the larger context in which the expression appears. – Leucas++
isn't an expression, it's an operator acting onp
, so the operand has to bep++
and notp
. – Uphroe*p++->str;
is clearly bad code. It's hard enough to understand that it generated a seven-plus paragraph question, didn't it? – Rodenticide(a=1)+(b=a);
. – Noctambulism