书上关于*(p++)表达式的几种变形形式的思考题
代码:
int main()
{
int a[10] = { 1,2,3,4,5,6,7,8,9,10 };
int *p = &a[3];
cout << "*p++ = " << *p++ << endl;
//因为++的优先级与*相等,但由于*和++运算符都是右结合性(从右向左),所以该表达式相当于*(p++),先返回p给*读取a[3]的值,p再自增,指向a[4]
cout << "*(++p) = " << *(++p) << endl;
//p先自增,指向a[5],再返回给*,输出a[5]的值6
cout << "*++p = " << *++p << endl;
//该表达式与上一行表达式一样,但是由于上面p已经指向a[5],所以这行p指向a[6],输出7
cout << "(*p)++ = " << (*p)++ << endl;
//先读取a[6]的值返回给<<输出7,a[6]的值再自增,变成8
cout << "++(*p)= " << ++(*p) << endl;
//先读取a[7]的值8返回给++,a[7]变成9,再输出9 //更正,应该是先读取a[6]的值8返回给++,a[6]变成9,再输出9
system("pause");
}
----------------------------------------------------------------------------------------------------
输出结果: