当前位置:网站首页>const用法精讲
const用法精讲
2022-06-29 14:36:00 【打酱油的;】
1.说在前面:
类似结构体函数参数通过指针来传递可以节省空间

但是存在一种情况,指针容易把原来的变量修改了,形成误操作,加const防止误操作,下面的p->age=100;会引起报错(但是可以间接修改)

2.使用场景:
//const修饰变量 void test01(){ //1. const基本概念const int i = 0; //i = 100; //错误,只读变量初始化之后不能修改 //a. 定义const变量最好初始化 const int j; //j = 100; //错误,不能再次赋值 //b. c语言的const是一个只读变量,并不是一个常量,可通过指针间接修改 const int k = 10; //k = 100; //错误,不可直接修改,我们可通过指针间接修改 printf("k:%d\n", k); int* p = &k; *p = 100; printf("k:%d\n", k); } //const 修饰指针(*号为界限,左定值,右定域) void test02(){ int a = 10; int b = 20; //const放在*号左侧 修饰p_a指针指向的内存空间不能修改,但可修改指针的指向 const int* p_a = &a; //*p_a = 100; //不可修改指针指向的内存空间 p_a = &b; //可修改指针的指向 //const放在*号的右侧, 修饰指针的指向不能修改,但是可修改指针指向的内存空间 int* const p_b = &a; //p_b = &b; //不可修改指针的指向 *p_b = 100; //可修改指针指向的内存空间 //指针的指向和指针指向的内存空间都不能修改 const int* const p_c = &a; } //2.const指针使用场景struct Person{ char name[64]; int id; int age; int score; }; //每次都对对象进行拷贝,效率低,应该用指针 void printPersonByValue(struct Person person){ printf("Name:%s\n", person.name); printf("Name:%d\n", person.id); printf("Name:%d\n", person.age); printf("Name:%d\n", person.score); } //但是用指针会有副作用,可能会不小心修改原数据 void printPersonByPointer(const struct Person *person){ printf("Name:%s\n", person->name); printf("Name:%d\n", person->id); printf("Name:%d\n", person->age); printf("Name:%d\n", person->score); } void test03(){ struct Person p = { "Obama", 1101, 23, 87 }; //printPersonByValue(p); printPersonByPointer(&p); } |
边栏推荐
猜你喜欢

FIFO implementation with single port RAM

Yi Ming ang Ke rushed to Hong Kong shares: loss of 730million in the year Lilly and sunshine life insurance were shareholders

在平面坐标上画斜线
![[practical chapter of correlation analysis] why can't Bi software do correlation analysis](/img/f2/4f99deb63b1beffae90b8a1fb270d1.png)
[practical chapter of correlation analysis] why can't Bi software do correlation analysis

Jet hydrogen technology rushes to the scientific innovation board: SAIC Group is the major shareholder to raise 1.06 billion yuan

Build your own website (19)

The first lesson on cloud - how easy is it to build a small broken station? The old driver of cloud computing will take you one hour to finish it

卫星运动的微分方程

校园跑腿微信小程序跑腿同学带直播新版源码

两个字的名字如何变成有空格的3个字符的名字
随机推荐
Are you still reading the log by command? Use kibana, one picture is better than ten thousand lines of log
Wei long updated the prospectus: the annual revenue of 4.8 billion founder liuweiping has a strong family color
阿里云体验有奖:使用PolarDB-X与Flink搭建实时数据大屏
模电 2个NPN管组成的恒流源电路分析
Yi Ming ang Ke rushed to Hong Kong shares: loss of 730million in the year Lilly and sunshine life insurance were shareholders
Chapter 5 text operation of canvas
《canvas》之第5章 文本操作
[blackduck] configure the specified Synopsys detect scan version under Jenkins
Chinese garbled code output from idea output station
华理生物冲刺科创板:年营收2.26亿 拟募资8亿
【Try to Hack】vulnhub DC2
精品商城拼团秒杀优惠折扣全功能完美双端自适应对接个人免签网站源码
两个字的名字如何变成有空格的3个字符的名字
云上第一课 | 建个小破站有多简单?云计算老司机带你一小时搞定
Huashu high tech rushes to the scientific innovation board: the actual controller xuxiaoshu and his son, who plan to raise 660million yuan, are both American nationals
JS will have variable promotion and function promotion
卫龙更新招股书:年营收48亿 创始人刘卫平家族色彩浓厚
技术沟通遇到3个为什么背后的逻辑
June 27 talk SofiE
Differences between @resource and @autowired annotations automatically injected: