当前位置:网站首页>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的值),可以修改
}
边栏推荐
猜你喜欢
Meaning and names of 12 nautical miles, 24 nautical miles and 200 nautical miles
QOS功能介绍
Flask框架——MongoEngine使用MongoDB数据库
MySQL 原理与优化:Update 优化
基于 Azuki 系列:NFT估值分析框架“DRIC”
从洞察到决策,一文解读标签画像体系建设方法论
fatal error C1083 Unable to open include file 'io.h' No such file
Redis -- Nosql
Problem solving-->Online OJ (19)
SWIG tutorial "two"
随机推荐
网络安全(加密技术、数字签名、证书)
中学数学建模书籍及相关的视频等(2022.08.09)
LeetCode_2598_剑指Offer Ⅱ 091.粉刷房子
高薪程序员&面试题精讲系列135之你对分布式是怎么理解的?CAP理论你知道吗?
PyTorch 多机多卡训练:DDP 实战与技巧
数学建模学习视频及资料集(2022.08.10)
Boss raises salary!Look at my WPF Loading!!!
消息称原美图高管加盟蔚来手机 顶配产品或超7000元
司空见惯 - 股市狠狠下跌后,何時能反弹?
Pytest framework optimization
使用mysq语句操作数据库
数据在内存中的存储
2022-08-10 Daily: Swin Transformer author Cao Yue joins Zhiyuan to carry out research on basic vision models
E. Cross Swapping (and check out deformation/good questions)
SWIG Tutorial "One"
“蔚来杯“2022牛客暑期多校训练营7
Flask框架——MongoEngine使用MongoDB数据库
Zijin Example
[Semantic Segmentation] DeepLab Series
systemui shield notification bar