What is the order of evaluation in ++*ptr++? Does it change when pointers and lvalues are involved in the operation?
If the precedence of a++ is higher than *a or ++a, then why is ++*a++ evaluated as first returning the incremented value then changing the pointer, rather than changing the pointer, then incrementing the value at the location.
Refrence for Precedence: https://en.cppreference.com/w/cpp/language/operator_precedence
arr = {9, 99, 999 };
int *ptr = arr;
std::cout << ++*ptr++ << '\t';
std::cout << *ptr;
I expected the output to be 100 100, but the actual output was 10 99.
The postfix increment a++ increments the pointer ptr, but returns the copy of ptr before the operation (see difference between prefix/postfix).
So it can be rewritten (as noted in Quimby's answer) as ++(*(ptr++)) and goes like:
Here the logic behind pre/post increment/decrement is explained well:
Pre-increment and pre-decrement operators increments or decrements the value of the object and returns a reference to the result. Post-increment and post-decrement creates a copy of the object, increments or decrements the value of the object and returns the copy from before the increment or decrement.
From: https://en.cppreference.com/w/cpp/language/operator_incdec
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With