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?
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 incrementsp
, but returns the value that was present before incrementing. In other words, this is equivalent of doing something like this:When styled like that, it's obvious why
10
is printed. If you want to incrementp
and print its dereference, you could use the prefix increment operator: