当前位置:网站首页>C语言const用法详解
C语言const用法详解
2022-07-27 01:16:00 【kunmu.】
️C语言const用法详解
近期在刷题时,总是会遇到const关键字,尤其是const与指针在一起的时候,对于我这样的小白来说,总是迷迷糊糊的,需要对着笔记才能写出答案。于是想着对const的用法进行自我归纳。希望也能让你从此不再迷惑!
参考文章-------------C语言中文网
const修饰变量
问大家一个问题,const修饰变量一定不能被修改吗?(PS:看到过腾讯一面问过,可惜我找不到图片了 )
看到这里,很多人会想,const修饰后的变量都为常变量了,怎么可以修改?
其实不然,我们可以通过指针的方式进行修改。
int main() { const int a = 10; int* p = &a; *p = 20; printf("%d\n",a); return 0; }

说明const修饰的变量是可以通过指针来进行修改的,但是需要知道的是,这种方法在VS中会警告!不建议使用!
这里我们只需要知道被const修饰的变量也是可以被修改的即可
const和指针
const也可以和指针变量一起使用,这样可以限制指针变量本身,也可以限制指针指向的数据。有以下的几种情况!
1、const放在 * 的左边
const放在 * 的左边,则指针变量所指向的数据不能通过指针变量改变。
但指针变量是可以改变的
int main() { int a = 10; int b = 20; const int* p = &a; //等价于 int const* p = &a; p = &b; //指针变量是可以改变 printf("%d\n", *p); return 0; }

2、const放在 * 的右边
const放在 * 的右边,则指针变量不能改变
![> [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iALI1sKw-1658676084479)(C:\Users\25584\AppData\Roaming\Typora\typora-user-images\image-20220724205723745.png)]](/img/97/98f5477eff866bb8b48f0f14b6f80e.png)
但指针变量所指向的对象是可以修改的
int main() { int a = 10; int* const p = &a; *p = 20; printf("%d\n", a); return 0; }
3、const放在 * 的 左右 边
const放在 * 的左右边,则指针变量不可被修改,且指针变量所指向的对象也不被修改,双重锁定。
const和函数形参
在C语言中,单独定义const变量没有明显的优势,完全可以使用 #define命令代替。const通常用在函数形参,如果形参一个指针,为了防止在函数内部修改指向的数据,就可以用const来限制
在C语言提供的标准库函数中,就存在很多函数的形参被const所修饰,下面是部分函数的原型:
size_t strlen (const char* str); int strcmp(const char* str1,const char* str2); char* strcat(char* destination,const char* source); char* strcpy(char* destination, const char* source); int printf ( const char * format, ... );
编程好习惯: 所以函数形参为指针时,若不希望指针所指向的内容被改变,则应及时在 * 的左边加上const 。
边栏推荐
- Integrated water conservancy video monitoring station telemetry terminal video image water level water quality water quantity flow velocity monitoring
- day6
- 单例模式(双检锁)
- Compile and use protobuf Library in vs2019
- 2513: Xiao Yong's academic score (common divisor problem)
- 185. 部门工资前三高的所有员工(必会)
- [hash table] question collection
- 力扣(LeetCode)207. 课程表(2022.07.26)
- On the prototype of constructor
- 5、 MFC view windows and documents
猜你喜欢

Common events of window objects

一体式水利视频监控站 遥测终端视频图像水位水质水量流速监测

After two years of graduation, I switched to software testing and got 12k+, and my dream of not taking the postgraduate entrance examination with a monthly salary of more than 10000 was realized

Ten thousand words long text, take you to understand the kubernetes network model

Cuteone: a onedrive multi network disk mounting program / with member / synchronization and other functions
全网最全的软件测试基础知识整理(新手入门必学)

QT编译出来的exe以管理员权限启动

A math problem cost the chip giant $500million!

Non global function of lua function

Portraiture5全新升级版磨皮滤镜插件神器
随机推荐
从ACL 2022 Onsite经历看NLP热点
coco test-dev 测试代码
阿里云技术专家杨泽强:弹性计算云上可观测能力的构建
Use the most primitive method to manually implement the common 20 array methods
2649: segment calculation
cocos小游戏实战-04-碰撞检测与NPC渲染
vs2019 中编译和使用 protobuf 库
浅浅梳理一下双轴快排(DualPivotQuickSort)
On the prototype of constructor
Go to export excel form
Shell (38): SSH port forwarding
数模1232
localStorage与sessionStorage
确定了,2022下半年软考报名8月开始
Shortcut keys commonly used in idea
食物链(DAY 79)
Marqueeview realizes sliding display effect
【flask】服务端获取客户端的请求头信息
“date: write error: No space left on device”解决
周全的照护 解析LYRIQ锐歌电池安全设计

![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-40xGkr6d-1658676084479)(C:\Users\25584\AppData\Roaming\Typora\typora-user-images\image-20220724210332415.png)]](/img/a9/b05de11719285a95f91011f9d6d300.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rxUyvnCX-1658676084480)(C:\Users\25584\AppData\Roaming\Typora\typora-user-images\image-20220724223925507.png)]](/img/74/92d360b9672ce62748e06986e8c90f.png)