C++的sizeof()与指针

本文通过C++代码示例详细介绍了不同数据类型(包括字符、整型、浮点型及指针)在内存中所占的字节数,展示了sizeof运算符的使用,并对比了变量与指针在内存占用上的差异。

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

/*
 *sizeof()与指针
 */
#include<iostream>
using namespace std;
void main()
{	//字符和字符串
	char str[] = "world";
	char* p = str;
	char i = 10;
	//整形
	int j = 10;
	short int k = 10;
	long int l = 10;
	//浮点型
	float m = 10;
	double n = 10;

	cout << "char str[]----"<<sizeof(str) << endl;     //字符数组包含\0
	cout << "char* p = str----" << sizeof(p) << endl;  //p为地址,32位
	cout << "char i = 10----" << sizeof(i) << endl;    //char占1个字节内存单元
	cout << "int j = 10----" << sizeof(j) << endl;
	cout << "short int k = 10----" << sizeof(k) << endl;
	cout << "long int l = 10----" << sizeof(l) << endl;
	cout << "float m = 10----" << sizeof(m) << endl;
	cout << "double n = 10----" << sizeof(n) << endl;
	system("pause");
	return;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

萌萌松

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值