常量指針即指針是常量的,一但聲明指向某個數據後不能被更改,但是指向的數據可以被更改。聲明格式如下:
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!