const int *p、int *const p、const int* const p的区别
来源:互联网 发布:vmware怎么安装mac os 编辑:程序博客网 时间:2024/05/16 19:14
一、首先
const int p = 10;int const p = 10; //是没有区别的
二、const int *p的用法
#include <stdlib.h> #include <stdio.h> #include <string.h> int main(int argc, char **argv) { int a = 1; int b = 2; const int *p; p = &a; p = &b; b = 3; //*p = 4; error: assignment of read-only location ‘*p’ printf("%d\n", *p); return 0; } //结果3Process returned 0 (0x0) execution time : 0.507 sPress any key to continue.//说明*p是常量,不可更改,但指针p还是变量,你想怎么 变都可以。
三、int *const p的用法
#include <stdlib.h> #include <stdio.h> #include <string.h> int main(int argc, char **argv) { int a = 1; int b = 2; int *const p = &a; //只能在声明的时候就给它赋初值,否则还是会报错的 //p = &b; error: assignment of read-only location ‘*p’ a = 3; printf("%d\n", *p); return 0; } //结果3Process returned 0 (0x0) execution time : 0.233 sPress any key to continue.//说明p是常量,也就是说p所指向的地址是不可以更改的,所以当把b的地址赋值给p时就会报错!但是p所指的地址内容是可以改变的。
四、const int *const p
#include <stdlib.h> #include <stdio.h> #include <string.h> int main(int argc, char **argv) { int a = 1; int b = 2; const int *const p = &a; //p = &b; //*p = 3; printf("%d\n", *p); return 0; } //说明这个就相当于以上两种情况的混合体,p是常量,所以不能把b的地址赋给p;同时*p也是常量,所以*p的内容不可更改!
五、总结
根据const的位置,先确定哪个是常量。
阅读全文
1 0
- const int *p,int *const p区别
- const int *p,int *const p区别
- const int *p,int *const p区别
- const指针,const int * p, int *const p,int const * p的区别
- const int *p、int *const p、const int* const p的区别
- int *p,cons int *p,int const *p,int * const p,const int * const p,int const * const p的区别
- 题目二:const int *p,const * int p,int const *p的区别还有int const *const p
- (const int *p)、(int const *p)、(int *const p)、(const int *const p)的区别
- const int *p与int *const p的区别
- const int *p与int *const p的区别
- const int *p 和 int* const p 的区别详解
- const int *p与int * const p的区别
- const int* p与int *const p的区别
- const int *p与int *const p的区别
- const int *p与int *const p的区别
- const int *p, int *const p的区别
- const int *p与int *const p的区别
- const int *p与int *const p的区别
- Mybatis的集合查询
- 计算系统启动过程(详细)
- STL算法之heap算法,已排序区间算法
- Python 访问控制
- druid-0.9.2 or 0.10.0 下载安装部署
- const int *p、int *const p、const int* const p的区别
- UI设计师怎么可以不了解微交互
- JS中的相等操作符(==)与全等操作符(===)
- linux- 从网卡启动自动安装系统 pxe
- linux 下编译安装svn服务端: 1: yum -y install subversion 2: 创建目录: mkdir -p /var/svn/shiyan 3: 创建仓库: svnadm
- 1.2.指令集对CPU的意义
- UI新手可以马上开始学的技能
- QFile读取文本文件中文乱码处理
- 移动App和远程服务器笔记(2)