当前位置:网站首页>const-modified pointer variable (detailed)
const-modified pointer variable (detailed)
2022-08-10 15:21:00 【Wink to】
const修饰的指针变量
一、未被const修饰的指针变量
Pointer variables can be modified
例1:
#include <stdio.h>
int main()
{
//未被const修饰的指针变量
int a = 10;
int b = 20;
int* p = &a;
*p = 20;//可修改
p = &b;//可修改
retrun 0;
}
二、constThe decorated pointer points to the content
指针 “指向的内容” 不可修改
例2:
#include <stdio.h>
int main()
{
// const如果放在*的左边
int a = 10;
int b = 20;
const int* p = &a;//const与最近的*结合,修饰的是指针 “指向的内容(即&a的值)” ,保证(&a的值)不能通过指针(*p)来改变
*p = 100;//原本的*p=10不能被修改,所以这里的*p=100是错误的,
p = &b;//But the contents of the pointer variable itself(原本&a)可以修改
return 0;
}
三、const修饰的指针变量本身
指针 “the content of the variable itself” 不可修改
例3:
#include <stdio.h>
int main()
{
// const如果放在*的右边,
int a = 10;
int b = 20;
int* const p = &a;//const与最近的p结合,修饰的是指针变量本身(即p),保证指针 “变量的内容(&a)” 不能被修改
p = &b;//原本的p=&a不能被修改,所以这里的p=&b是错误的!
*p = 100;//但指针 “指向的内容” (即&a的值),可以修改
}
边栏推荐
猜你喜欢
随机推荐
"NIO Cup" 2022 Nioke Summer Multi-School Training Camp 7
640. Solving Equations: Simple Simulation Problems
PAT甲级 1014 排队等候(队列大模拟+格式化时间)
Problem solving-->Online OJ (19)
epoll学习:思考一种高性能的服务器处理框架
微信扫码登陆(1)—扫码登录流程讲解、获取授权登陆二维码
“蔚来杯“2022牛客暑期多校训练营7
win2012安装Oraclerac失败
Summary of tensorflow installation stepping on the pit
TCP为什么是三次握手和四次挥手?
JS entry to proficient full version
pm2之静态文件服务
scala集合
【语义分割】DeepLab系列
2022-08-10 Daily: Swin Transformer author Cao Yue joins Zhiyuan to carry out research on basic vision models
scala 10种函数高级应用
程序员=加班??——掌握时间才能掌握人生
Systemui status bar to add a new icon
pm2 static file service
Do not access Object.prototype method ‘hasOwnProperty‘ from target object....