int const * pci和int * const cpi的区别
来源:互联网 发布:格式化的数据恢复 编辑:程序博客网 时间:2024/05/22 10:23
最近在看《C和指针》,里面讲到int const * pci, 和int * const cpi的区别,感觉略有意思。
测试代码如下:
#include <stdio.h>int main(){int i = 10, j = 20;int * const a = &i;int const * b = &i;int const * const c = &i;printf("%d %d %d\n", *a, *b, *c);i = 100;printf("%d\n",*a);printf("%d\n",*b);printf("%d\n",*c);*a = j;//*b = j;//ERROR//*c = j; //ERRORprintf("%d\n",*a);printf("%d\n",*b);//a = &j; //ERRORb = &j;//c = &j; //ERRORprintf("%d\n",*a);printf("%d\n",*b);printf("%d\n",*c);return 0;}
int * const cpi; cpi是一个常量,而且是一个指向int型的指针,一旦cpi指向一个地址就不能再指向另一个地址。所以呢cpi的值不能改(line 20 error),但是它指向的变量的内容可以改(line 14 right)。
int const * pci; pci是一个指针,指向一个int型常量的指针,这个指针可以指向其他的地址,但是它指向的地址里面的变量值不能修改。所以pci的值可以修改(line 21 right),但是它指向的是一个int型常量,故不能修改(line 15 error).
0 0
- int const * pci和int * const cpi的区别
- int const *pci 与 int *const pci的区别
- const int* 和 int const*的区别?
- const int*和int const*的区别
- Const int *, int const *, 和int * const 的区别:
- int *const 和 int const*的区别 (指针 和 const)
- const int *x 和 int * const x, const int& x和 int const &x的区别
- const int & 和 int 的区别
- const int 和INT const区别
- const int *p 和 int* const p 的区别详解
- const int * pt和int * const pt的区别
- int const * p1和 const int * p1的区别
- C++ const int* p 和 int * const p的区别
- 关于const int*, int const*以及int *const的区别
- const int * 与int const * 与int * const的区别
- 关于const int*, int const*以及int *const的区别
- const int *与int *const 的区别
- const int * p 和 int const * p 和 int * const p 的区别
- 扑克牌的顺子
- Spring笔记三
- JAVA中运用数组的四种排序方法
- 创建类模式
- Android emulator 手机模拟器使用详述
- int const * pci和int * const cpi的区别
- UI Design Basic :0.2 starting and stopping
- rlimit Linux下限制资源的使用(Limiting Resource Usage)
- 【matlab】plot
- 黑马程序员--java_交通系统代码思路分析
- 那时,我们不懂爱
- 硬盘Boot Sector
- css弹出框 提示信息
- DB2停止单个数据库的方法