#include <stdio.h>
int main()
{
#1
//const used in data which means the data is unchangable
//and it must assigned when defined
const int x = 10;
// x = 6; //assignment of read-only variable 'x'
int b = 0;
#2
//const used in pointer means that only the point itself is unchangable
//but the data it point to can be modified
//and it can assigned later in the programme
const int * a;
a = &b;
int c = 5,*d;
// d = a; //因为 a 比 b 多一个限定(const,即a比b大,姑且这么解释吧),所以不能把a赋值给b
printf("%d\n",*a);
a = &c;
printf("%d\n",*a);
#3
char **cp;
const char **ccp;
// ccp = cp;//error: invalid conversion from `char**' to `const char**'
//这里的问题比较复杂,不同于17和21行的赋值,
//这句赋值相当于将一个指向int的指针赋给指向float的指针,
//因为**cp指向的指针是char*,而**ccp指向的指针是 const char*
#4
char *cp1;
const char *ccp1;
ccp1 = cp1;//因为 ccp1 比 cp 多一个限定(同上),所以可以把cp赋值给ccp
}
《C专家编程》中关于const的笔记
最新推荐文章于 2022-12-19 23:33:15 发布