1.定义常量
const int MAX VAL= 23;
const double Pi= 3.14;
const char*SCHOOL NAME,"Peking University";
2.定义常量指针
注意(1):不可以通过常量指针修改其指向的内容
int n,m;
const int * p= & n;
*p=5;//编译出错
n=4;//ok
p=&m;//ok,常量指针的指向可以发生变化
(2)不能把常量指针赋值给非常量指针,反过来可以
const int * p1;
int * p2;
p1 = p2;//ok
p2= p1;//error
p2=(int*)p1; //ok,强制类型转换
(3)函数参数为常量指针时,可避免函数内部不小心改变参数指针所指地方的内容
void MyPrintf( const char * p )
strcpy( p,"this"); //编译出错
printf("%s",p);//ok