-1

The site https://www.geeksforgeeks.org/difference-between-p-p-and-p/ says

Precedence of prefix ++ and * is same. Associativity of both is right to left.

Precedence of postfix ++ is higher than both * and prefix ++. Associativity of postfix ++ is left to right.

1st Code Sample:

int x[4] = {0, 1, 2, 3};
int *ptr = x;

cout << x[0] << " at " << &x[0] << "\n";
cout << x[1] << " at " << &x[1] << "\n";
cout << x[2] << " at " << &x[2] << "\n";
cout << x[3] << " at " << &x[3] << "\n";
cout << "*ptr = " << *ptr << " at " << ptr << "\n";
cout << "*++ptr = " << *++ptr << " at " << ptr << "\n";
cout << "++*ptr = " << ++*ptr << " at " << ptr << "\n";
cout << "*ptr++ = " << *ptr++ << " at " << ptr << "\n";

This produces the output:

0 at 0012FF1C
1 at 0012FF20
2 at 0012FF24
3 at 0012FF28
*ptr = 0 at 0012FF1C
*++ptr = 1 at 0012FF20
++*ptr = 2 at 0012FF20
*ptr++ = 2 at 0012FF24

In the last cout statement inspite of post increment pointer it first increments the pointer "ptr" value before being used.

2nd Code Sample:

int cd = 7;
cout << "cd = " << cd << " at " << &cd<< "\n";
cout << "++cd = " << ++cd << " at " << &cd << "\n";
cout << "cd++ = " << cd++ << " at " << &cd << "\n";

This procudes an output:

cd = 7 at 0012FF04
++cd = 8 at 0012FF04
cd++ = 8 at 0012FF04

In the last cout statement here note that cd incremented and then accessed inspite of using post increment operator.

3rd Code Sample:

int c = 10;
int d = 1;
cout << c << " at " << &c << "\n";
int e = c+++d;
cout << c << " at " << &c << "\n";
cout << d << " at " << &d << "\n";
cout << e << " at " << &e << "\n";

This gives the output:

10 at 0012FF04
11 at 0012FF04
1 at 0012FEF8
11 at 0012FEEC

We see that ++ increments vars value AFTER it is accessed in the statement.

Question is in the 3rd code sample unlike in first two code samples why post increment operator did not increase the value of variable "c" before accessing it? Why in the last code sample the variable did not received the value of 12?

Community
  • 1
  • 1
RKum
  • 620
  • 2
  • 9
  • 23
  • 2
    Is `cout << "*ptr++ = " << *ptr++ << " at " << ptr << "\n";` defined though? – François Andrieux Sep 17 '18 at 18:50
  • 1
    @FrançoisAndrieux Which version of C++? :) – Yakk - Adam Nevraumont Sep 17 '18 at 18:51
  • @Yakk-AdamNevraumont Why and how has that changed? – François Andrieux Sep 17 '18 at 18:52
  • Rkum, why are you printing out addresses of automatic storage variables? That seems like meaningless noise. Print out their names. Also, from your question, I don't see the point of the first two code samples if you are just asking about the 3rd. Third, `int e = c+++d;` -- unless you are specifically asking how `c+++d` is parsed, using it is frankly hostile to your reader. And you don't appear to be asking that question. Please provide a [MCVE] that focuses on your question and only your question, make the code as simple as you can so it still contains the issue you don't understand. – Yakk - Adam Nevraumont Sep 17 '18 at 18:52
  • @Yakk-AdamNevraumont more importantly, why is he writing `int e = c+++d;`? Is this code obfuscation exercise? – Innocent Bystander Sep 17 '18 at 18:53
  • @FrançoisAndrieux C++17 changed how the order of operations around arguments to operators gets evaluated significantly. Prior to C++17 it was very insane, after C++17 it is only modestly insane. – Yakk - Adam Nevraumont Sep 17 '18 at 18:56
  • @πάνταῥεῖ Can you point out which operation is causing you to use the dupe you are using? – NathanOliver Sep 17 '18 at 18:57
  • @NathanOliver I already reopened the question. – πάντα ῥεῖ Sep 17 '18 at 18:58
  • 1
    Looks like it's no longer UB since c++17 if [cppreference](https://en.cppreference.com/w/cpp/language/eval_order#Undefined_behavior) is to be believe. Guess I'll have to re-learn all that. Edit : Though I can't find what changed. – François Andrieux Sep 17 '18 at 19:01
  • Hey Yakk - Adam Nevraumont in the 3rd sample code in the statement int e = c+++d; value of variable c is accessed in the expression and then increased. But the same does not happen in first two code sample. This is the reason why I had to give the first two code samples. – RKum Sep 17 '18 at 19:13
  • 1
    Followup : https://stackoverflow.com/questions/38501587/what-are-the-evaluation-order-guarantees-introduced-by-c17 – François Andrieux Sep 17 '18 at 19:14

1 Answers1

0

"why post increment operator did not increase the value of variable "c" before accessing it"

Because it's a post increment operator. It evaluates its operand and increments it afterwards.

So int e = c+++d; is interpreted as "assign c + d to e, and also increment c."

In the first two examples you've written code that changes the value of a variable, and evaluates that variable, without any sequence points to restrict the order of operations.

Tim Randall
  • 2,794
  • 1
  • 10
  • 32
  • Thanks Tim for the reply. This is what we expect. But the same does not happen in first two code sample. Why in the first two sample codes it incremented it first and evaluated the operand? – RKum Sep 17 '18 at 19:17
  • 1
    Thanks. I've added to the answer – Tim Randall Sep 17 '18 at 19:22