I am trying to understand the usage of *ptr++
. Below is the code I have used (Scenario 1)
#include<iostream>
int main()
{
int a[5];
a[0] = 3;
a[1] = 2;
a[2] = 4;
a[3] = 7;
a[4] = 9;
int* ptr;
ptr = &a[0];
std::cout << *ptr << " ";
std::cout << *ptr++ ;
}
I have read that the precedence is '++' first, and then dereferencing. So I expected that the output would be 3 2
. However, the output I obtained is 3 3
.
Now, in Scenario 2, if I try
int* ptr;
ptr=&a[0];
*ptr++;
std::cout<< *ptr;
(with all lines of code before int* ptr
remaining the same), I obtain the output as 2
.
So how is *ptr++
working, and in Scenario 2, isn't *ptr++
working the same as a simple ptr++
?