栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++sizeof专题------搞清楚类型

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++sizeof专题------搞清楚类型

	char c = 'e';
	short s;
	int i;
	double d;
	float f[3];
	bool g = true;
	cout << sizeof(c) << endl;
	cout << sizeof(s) << endl;
	cout << sizeof(i) << endl;
	cout << sizeof(d) << endl;
	cout << sizeof(f) << endl;
	cout << sizeof(g) << endl;

char str[ ] = “Hello”;
char *s = “Hello”;
char *p = str;
char *  q, t;

	int a[2][3];
	cout << sizeof(a) << endl;
	cout << sizeof(a[1]) << endl;
	cout << sizeof(a[0][2]) << endl;

 注:

a是一个二维数组,二维数组储存的是一维数组,也就是说,因为a的类型是二维数组存储六个数(2x3)所以a的大小就是(4X6)

而a[1]储存的才是数组元素所以大小为(3X4)

void* p = malloc(100);
cout << sizeof(p) << endl;

 注:因为p是一个指向100个字节分配内存的首地址所以说p在本质上就是个int类型的数组(地址)

	double* p = new double[100];
	cout << sizeof(p) << endl;
	cout << sizeof(*p) << endl;

 注:*p的本质是double所以大小为8

double** p = NULL;
cout << sizeof(p) << endl;
cout << sizeof(*p) << endl;

 注:不论是**p还是*p都是地址都是4(int类型)

    double**** p = NULL;
	cout << sizeof(p) << endl;
	cout << sizeof(*p) << endl;
	cout << sizeof(**p) << endl;
	cout << sizeof(***p) << endl;
	cout << sizeof(****p) << endl;

 注:前面四个都是指针,后面最后一个是数值

char* p[2];
cout << sizeof(p) << endl;
cout << sizeof(*p) << endl;
cout << sizeof(p[0]) << endl;
cout << sizeof(*p[0]) << endl;

 注:*p[2]指的是数组指针

详情请看指针数组与数组指针详解_men_wen的博客-CSDN博客_指针数组和数组指针

short(*p)[3];
cout << sizeof(p) << endl;
cout << sizeof(*p) << endl;
cout << sizeof((*p)[1]) << endl;
cout << sizeof(p[0][1]) << endl;

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/779375.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号