您的位置:首页 > 编程语言 > C语言/C++

C++常量指针与常量数据

2015-09-14 17:16 232 查看
常量指针即指针是常量的,一但声明指向某个数据后不能被更改,但是指向的数据可以被更改。声明格式如下:

int demo = 0;
int * const p = &demo;


常量数据是指数据是常量的,一但被初始化后不能被修改。声明格式如下:

int demo = 0;
const int * p = &demo;


常量指针与常量数据配合使用的区别如下:

int demo = 0,test = 1;
//常量指针
int * const p = &demo;
*p = 1; //right!
p = &test; //wrong!


int demo = 0,test = 1;
//常量数据
const int *p = &demo;
*p = 1;//wrong!
p = &test; //right!


int demo = 0,test = 1;
const int * const p = &demo;
*p = 1;//wrong!
p = &test; //wrong!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: