当前位置:网站首页>const修饰的指针变量(详解)
const修饰的指针变量(详解)
2022-08-10 14:48:00 【Wink向】
const修饰的指针变量
一、未被const修饰的指针变量
指针变量可以被修改
例1:
#include <stdio.h>
int main()
{
//未被const修饰的指针变量
int a = 10;
int b = 20;
int* p = &a;
*p = 20;//可修改
p = &b;//可修改
retrun 0;
}
二、const修饰的指针指向内容
指针 “指向的内容” 不可修改
例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;//但是指针变量本身的内容(原本&a)可以修改
return 0;
}
三、const修饰的指针变量本身
指针 “变量本身的内容” 不可修改
例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的值),可以修改
}
边栏推荐
猜你喜欢
随机推荐
Problem solving-->Online OJ (19)
Mysql语句分析、存储引擎、索引优化等详情
Basic learning of XML
Redis -- Nosql
解读STEAM教育中的表现性评价
从洞察到决策,一文解读标签画像体系建设方法论
领域驱动模型设计与微服务架构落地-从项目去剖析领域驱动
640. Solving Equations: Simple Simulation Problems
MySQL advanced (thirty-three) MySQL data table adding fields
The a-modal in the antd component is set to a fixed height, and the content is scrolled and displayed
QOS功能介绍
1004 (tree array + offline operation + discretization)
奢侈品鉴定机构小程序开发制作功能介绍
12海里、24海里、200海里的意义及名称
解题-->在线OJ(十九)
Do not access Object.prototype method ‘hasOwnProperty‘ from target object....
QOS function introduction
Summary of tensorflow installation stepping on the pit
公网IP和内网IP的区别[通俗易懂]
Azure IoT Partner Technology Empowerment Workshop: IoT Dev Hack





![[Semantic Segmentation] DeepLab Series](/img/3d/f06c04522db40ad17f7f725613a035.png)



