指针常量和常量指针
// Example program
typedef char *ptr;
int main()
{
//p是指向char的常量指针
const ptr p = nullptr;
p =0;
*p = 9;
//t是指向char的常量指针
char *const t = nullptr;
t = 0;
*t = 9;
//s是指向char的常量指针
constexpr char *s = nullptr;
s = 0;
*s = 9;
//q是指向char类型常量的指针
const char *q = nullptr;
q =0 ;
*q =9;
}
编译结果:
In function ‘int main()’:
8:9: error: assignment of read-only variable ‘p’
13:7: error: assignment of read-only variable ‘t’
18:7: error: assignment of read-only variable ‘s’
24:8: error: assignment of read-only location ‘* q’