int constTest(void)
{
int a[10]={1,2,3,4,5,6,7,8,9,10};
const int*p1=&a[0];//p1指向的值不能变,p1本身可以变
int const*p2=&a[1];//p2同上
int*const p3=&a[2];//p3本身不能变,p3指向的值可以变
const int*const p4=&a[4];//p4本身和指向的值都不能变
p1=&a[9];//correct
*p1=a[1];//error
p2=&a[9];//correct
p2=&a[9];//error
*p3=a[9];//correct
p3=&a[9];//error
p4=&a[9];//error
*p4=a[9];//error
return 0;
}
有一个诀窍,就是先忽略掉类型名int,看const离哪一个近,就是修饰哪一个,
例如 const int*p1 不看 int,const修饰的是*p1,说明 它修饰的*p1不能变,即p1指向的值不能变,而p1本身可以变(当然因为p1本身变了所以它指向变了,但不能直接修改p1指向的值)