1 定义常量
const int MAX_VAL = 23;
comst double Pi = 3.14;
comst char *SCHOOL_NAME = "Peking University";
多用const少用define,因为const有类型,便于类型检查。
2 定义常量指针
不可通过常量指针修改其指向的内容
int n, m;
const int *p = & n;
* p = 5;//编译出错
n = 4; //OK
p = & m;//OK, 常量指针的指向可以变化
不能把常量指针赋值给非常量指针,反过来可以
const int *P1;
int *p2;
p1 = p2; //OK
p2 = p1; //error
p2 = (int *)p1; //OK,强制类型转换
函数参数为常量指针时,可避免函数内部不小心改变参数指针所指地方的内容
void MyPrintf(const char *p)
{
strcpy(p, "this"); //编译出错
printf("%s", p); //OK
}