[Solved] Precedence of post increment [closed]


I’m actually going to answer this question, even though it’s true that “the OP simply needs to compile the code and run it”, because it isn’t obvious to a beginner that this is one of the cases where that gives the right answer. People get dinged all the time on this site for asking why superficially similar code (that does have undefined behavior) gives a surprising answer.

int j = --*p++;

means

int j = *p - 1;
*p = j;
p += 1;

That is, *p (the value that p points to) is decremented, the decremented value is written to j, and then p itself is incremented.

There is no undefined behavior here because the -- and ++ operators in the original form are acting on two different data objects. The expressions that have undefined behavior due to multiple use of the side-effect operators all involve modifying the same data object twice in the same expression (“without an intervening sequence point”).

(If you’re wondering why the -- applies to *p but the ++ applies only to p, the answer is “because that’s what the grammar of the language says. Sorry, this is just one of those things you have to memorize.”)

0

solved Precedence of post increment [closed]