常量指针即指针是常量的,一但声明指向某个数据后不能被更改,但是指向的数据可以被更改。声明格式如下:
1 int demo = 0; 2 int * const p = &demo;
常量数据是指数据是常量的,一但被初始化后不能被修改。声明格式如下:
1 int demo = 0; 2 const int * p = &demo;
常量指针与常量数据配合使用的区别如下:
1 int demo = 0,test = 1; 2 //常量指针 3 int * const p = &demo; 4 *p = 1; //right! 5 p = &test; //wrong!
int demo = 0,test = 1; //常量数据 const int *p = &demo; *p = 1;//wrong! p = &test; //right!
1 int demo = 0,test = 1; 2 const int * const p = &demo; 3 *p = 1;//wrong! 4 p = &test; //wrong!