当前位置:网站首页>2、const 型指针
2、const 型指针
2022-07-02 11:21:00 【Little BigUs】
const 限定符用于标记一个变量为只读,但是当修饰指针的时候有点复杂。
首先,const int *pa
和int const *pa
的效果都是一样的,这两个完全等价,这个const修饰的是*pa这个整体,也就是说*pa这个整体是只读的,但是pa(不加 *)是可以修改的。它指向的变量也是可以修改的。举个例子
int a = 100;
const int *pa = &a;
printf("%d\n",*pa);//100
a = 200;
printf("%d\n",*pa);//200
int a = 100;
int b = 200;
const int *pa = &a;
printf("%d\n",*pa);//100
pa = &b;
printf("%d\n",*pa);//200
int a = 100;
const int *pa = &a;
printf("%d\n",*pa);//100
*pa = 200;//报错,*pa是const型的,只读
而 int * const pa
中pa也是一个指针,但是这个const修饰的是pa而不再是*pa这个整体了 ,也就是说,此时可以通过*pa来修改a的值了,但是pa是不可变的,也就是pa只能指向a了,不能再把他指向b,因为pa的值就是它指向的目标的地址,这是只读的。
当然我们也可以用const int * const pa
这种格式,这样的话就是pa和*pa都是只读的。
边栏推荐
- 【虹科技术分享】如何测试 DNS 服务器:DNS 性能和响应时间测试
- Talk about idempotent design
- [deep learning] simple implementation of neural network forward propagation
- Design and implementation of car query system based on php+mysql
- Analysis of CPU surge in production environment service
- Whole house Wi Fi: a pain point that no one can solve?
- Origin plots thermogravimetric TG and differential thermogravimetric DTG curves
- QT - make a simple calculator - realize four operations
- 博睿数据一体化智能可观测平台入选中国信通院2022年“云原生产品名录”
- Golang quickly generates model and queryset of database tables
猜你喜欢
Essential elements of science fiction 3D scenes - City
Fabric.js 自由绘制圆形
In 2021, the global styrene butadiene styrene (SBS) revenue was about $3722.7 million, and it is expected to reach $5679.6 million in 2028
TeamTalk源码分析之win-client
自定义事件,全局事件总线,消息订阅与发布,$nextTick
Chinese science and technology from the Winter Olympics (III): the awakening and evolution of digital people
默认插槽,具名插槽,作用域插槽
Default slot, named slot, scope slot
Daily learning 3
<口算練習機 方案開發原理圖>口算練習機/口算寶/兒童數學寶/兒童計算器 LCD液晶顯示驅動IC-VK1621B,提供技術支持
随机推荐
Route (II)
docker mysql
[development environment] 010 editor tool (tool download | binary file analysis template template installation | shortcut key viewing and setting)
Pychart connects to the remote server
《可供方案开发》口算训练机/数学宝/儿童口算宝/智能数学宝 LCD液晶显示驱动IC-VK1622(LQFP64封装),原厂技术支持
Fabric.js 动态设置字号大小
联合搜索:搜索中的所有需求
Data Lake (11): Iceberg table data organization and query
2022 home projector preferred! Dangbei F5 brings the ultimate audio-visual experience with its powerful audio-visual effect
Do you know that there is an upper limit on the size of Oracle data files?
快解析:轻松实现共享上网
全屋Wi-Fi:一个谁也解决不好的痛点?
Some interview suggestions for Android programmers "suggestions collection"
Development and design of animation surrounding mall sales website based on php+mysql
Daily learning 3
kaggle如何使用utility script
mongodb的认识
Openharmony notes --------- (4)
默认插槽,具名插槽,作用域插槽
关于Flink框架窗口(window)函数最全解析