const限定符和指针的用法实例

来源:互联网 发布:python在spss中的应用 编辑:程序博客网 时间:2024/05/23 16:05

刚开始看《Effective C++》这本书,里面讲到const。我按照书上讲的,简单的写了个例子。如下。备忘。

#include <stdio.h>int main(){/*//下面const的用法表示const修饰的是p所指向的值是常量int i = 10;int k = 20;const int *p = &i;printf("%x\n",p);//*p = 20; //这一句是错误的,*p所指向的值是常量p = &k;printf("%x\n",p);printf("%d",*p);*///下面演示const修饰指针本身的情况int m = 100;int n = 200;char * const p = (char * const)&m; //如果不加(char * const)会提示错误printf("%x\n",p);printf("%d\n",*p);//p = (char * const)&n;  //提示错误,不能给常量p赋值*p = 50;printf("%x\n",p);printf("%d\n",*p);getchar();return 0;}


1 0