The C++ standard states
Section 1.9/15 [intro.execution] : Except where noted, evaluations of operands of individual operators and of subexpressions of individual
expressions are unsequenced. (...) If a side effect on a scalar object is unsequenced relative to either another side effect on the
same scalar object or a value computation using the value of the same
scalar object, the behavior is undefined.
++*ptr--
and *ptr
are unsequenced subexpressions of the same expression using the same object: nothing guarantees that they are evaluated from left to right. So according to the standard, this results in undefined behaviour. Your result tend to show that your compiler chooses to evaluate first *ptr
and then ++*ptr--
.
Edit: ++*ptr--
is ++(*ptr--))
. Here the operand of operator ++
also uses object ptr
on which --
does a side effect. So this is undefined behaviour as well. It appears that in your case, the compiler first evaluates *ptr--
which results in 40 and a decremented ptr
, and then applies ++
on the dereferenced decremented pointer (i.e. 30 incremented by 1).