C++ 指针和const纠缠不清

本文详细解析了 C++ 中不同类型的指针用法,包括指向常量的指针、指针常量以及指向常量的指针常量等,并通过示例代码展示了它们的特点与使用限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

神必

#include<cstdio>
#include<iostream>

using namespace std;

int main() {
	
	//指向常量的指针,可以变地址,不能变内容 
	const int *A = new int(1);
	A = new int(2);
	//*A = 3; nope
	
	//----------------------同上 
	int const *B = new int(1);
	B = new int(2);
	
	//-----------------------指针常量 ,可以改内容,不能改地址,类似于final 
	int* const C = new int(1);
	*C = 2;
	//C = new int(3); nope
	
	//-----------------------指向常量的指针常量 不能改地址 && 不能改内容 
	const int* const D = new int(2); 
	//D = new int(2); nope
	//*D = 3          nope

	return 0;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值