4、对指针的操作
二者修饰指针类型时,作用不同。
typedefint* pint;
#define PINT int*
int i1 =1, i2 =2;
const pint p1 =&i1; //p不可更改,p指向的内容可以更改,相当于 int * const p;
const PINT p2 =&i2; //p可以更改,p指向的内容不能更改,相当于 const int *p;或 int const *p;
pint s1, s2; //s1和s2都是int型指针
PINT s3, s4; //相当于int * s3,s4;只有一个是指针。
voidTestPointer()
{
cout <<"p1:"<< p1 <<" *p1:"<<*p1 << endl;
//p1 = &i2; //error C3892: 'p1' : you cannot assign to a variable that is const
*p1 =5;
cout <<"p1:"<< p1 <<" *p1:"<<*p1 << endl;
cout <<"p2:"<< p2 <<" *p2:"<<*p2 << endl;
//*p2 = 10; //error C3892: 'p2' : you cannot assign to a variable that is const
p2 =&i1;
cout <<"p2:"<< p2 <<" *p2:"<<*p2 << endl;
}
结果:
p1:00EFD094 *p1:1
p1:00EFD094 *p1:5
p2:00EFD098 *p2:2
p2:00EFD094 *p2:5