void list(int a[],int len)
{
cout<<"the value of arrary:"<<endl;
for(int i=0;i<len;i++)
{
cout<<a[i]<<endl;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int a[]={1,3,5,7};
int *p=a;
cout<<"*p++:/t"<<*p++<<endl;//先*P,然后p++
list(a,4);
p=a;
cout<<"*(p++):/t"<<*(p++)<<endl;//先*P,然后p++
list(a,4);
p=a;
cout<<"(*p)++:/t"<<(*p)++<<endl;//先*p,然后p所指向的内容(*p)加1
list(a,4);
cin.get();
}
output:
*p++: 1
the value of arrary:
1
3
5
7
*(p++): 1
the value of arrary:
1
3
5
7
(*p)++: 1
the value of arrary:
2
3
5
7
对这个的不理解主要还是对运算符++的不理解造成的,后置++是用++前的结果做为表达式的值的先计算,再加加的,这个也许大家都明白,可是若是加个括号呢?*(p++),加了个括号就有人相当然的理解为要先p++再解引用,其实不然,这和*p++是一样的,都城是先*P,然后p++,* 和++同优先级,右结合,所以加不加这个括号效果是一样的,如果这样还不好理解我们再举个例子也许好理解点。
int i=2;
int b=3*i++; //++优先级高于* int b=3*(i++);
output: 6 output:6
结论:
*p++ 等于*(p++)
(*P)++ p所指内容加1