This is classic undefined behaviour! There is no guarantee in the C++
standard about the order of evaluation of the operands of the +
operator in y=**p + ++(**p)
.
I tested your code in MSVC
and clang-cl
and I get the output: 6 6 6 12
- which suggests (as you expect) that ++(**p)
is evaluated first. However, on your compiler, it seems that the LHS is evaluated first.
From the cppreference site linked in the comments by Scheff:
Order of evaluation of any part of any expression, including order of
evaluation of function arguments is unspecified (with some exceptions
listed below). The compiler can evaluate operands and other
subexpressions in any order, and may choose another order when the
same expression is evaluated again. There is no concept of
left-to-right or right-to-left evaluation in C++….
PS: Interestingly, changing to y = ++(**p) + **p;
also gives 6 6 6 12
as the output.