int x =5; const int * const p = &x; const int &q=x; int const *next=&x; const int *j=&x;则有语法错误的是()
答案:ABD
A. const int * const p = &x;
中指针p及p指向的int类型数据都被const修饰,即*p
与p
都为const属性无法修改
error: assignment of read-only location ‘*(const int*)p’ *p = 1;
* p =1;
语法错误B. const int &q=x;
是指向常量的引用,使用时必须初始化,而且初始化后,引用值不可以改变,引用的常量也不能改变
q
,x
不受影响error: increment of read-only reference ‘q’ q++;
q++;
语法错误C. int const *next=&x;
中const
修饰整型数 int即*next
*next
,x
仍然是变量next
没有被const修饰因此可以被next++
改变,语法正确D. const int *j=&x;
中const int *
同int const *
const修饰*j
error: increment of read-only location ‘* j’ (*j)++;
(*j)
为const属性无法修改,故语法错误