int a[3]={10,20,30};
int* p = a;
cout << *p++ << endl;
According to wikipedia, suffix ++
has higher precedence than dereference, *p++
should run p++
first and then dereference and the result should be 20, but why the actual result is 10?
p++
uses the postfix increment operator. I.e., it increments p
, but returns the value that was present before incrementing. In other words, this is equivalent of doing something like this:
int a[3]={10,20,30};
int* p = a;
int* q = p;
++p;
cout << *q << endl;
When styled like that, it's obvious why 10
is printed.
If you want to increment p
and print its dereference, you could use the prefix increment operator:
int a[3]={10,20,30};
int* p = a;
cout << *(++p) << endl;