当前位置:网站首页>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都是只读的。
边栏推荐
- Design and implementation of car query system based on php+mysql
- 提示:SQL Server 阻止了对组件‘Ad Hoc Distributed Queries ‘的STATEMENT ‘OpenRowset/OpenDatasource“”
- Multi rotor aircraft control using PID and LQR controllers
- Fabric.js 动态设置字号大小
- Fabric. Keep the original level when JS element is selected
- Yyds dry goods inventory software encryption lock function
- MQ教程 | Exchange(交换机)
- 快解析:轻松实现共享上网
- Some interview suggestions for Android programmers "suggestions collection"
- 2022家用投影仪首选!当贝F5强悍音画效果带来极致视听体验
猜你喜欢

The conference on the growth of all things was held in Hangzhou, and dangbei was selected into the top 100 list of future unicorns in China in 2022

什么是 eRDMA?丨科普漫画图解

Design and implementation of car query system based on php+mysql

The most complete analysis of Flink frame window function

<口算练习机 方案开发原理图>口算练习机/口算宝/儿童数学宝/儿童计算器 LCD液晶显示驱动IC-VK1621B,提供技术支持

自定义事件,全局事件总线,消息订阅与发布,$nextTick

Getting started with QT - making a simple calculator

Do you know that there is an upper limit on the size of Oracle data files?

MQ教程 | Exchange(交换机)

Whole house Wi Fi: a pain point that no one can solve?
随机推荐
路由(二)
Dangbei projection 4K laser projection X3 Pro received unanimous praise: 10000 yuan projector preferred
Whole house Wi Fi: a pain point that no one can solve?
Word frequency statistics & sorting
c# 水晶报表打印
TeamTalk源码分析之win-client
Data consistency between redis and database
Qt原代码基本知识
抓包工具fiddler学习
The use of TestNG, the testing framework (II): the use of TestNG XML
Fabric.js 自由绘制椭圆
php链表创建和遍历
In 2021, the global TCB adapter revenue was about $93 million, and it is expected to reach $315.5 million in 2028
Fabric.js 动态设置字号大小
Fabric.js 橡皮擦的用法(包含恢复功能)
关于Flink框架窗口(window)函数最全解析
2022 home projector preferred! Dangbei F5 brings the ultimate audio-visual experience with its powerful audio-visual effect
BeanUtils -- shallow copy -- example / principle
What is erdma? Popular science cartoon illustration
P1042 [noip2003 popularization group] Table Tennis