0
点赞
收藏
分享

微信扫一扫

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

慕容冲_a4b8 2022-03-26 阅读 41
c++
	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;

 

举报

相关推荐

0 条评论