1
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?

4

1 回答 1

3

p++使用后缀增量运算符。即,它递增p,但返回递增之前存在的值。换句话说,这相当于做这样的事情:

int a[3]={10,20,30};
int* p = a;
int* q = p;
++p;
cout << *q << endl;

当这样设计时,很明显为什么10要打印。如果要递增p并打印其取消引用,可以使用前缀递增运算符:

int a[3]={10,20,30};
int* p = a;
cout << *(++p) << endl;
于 2014-12-12T15:03:13.847 回答