C++基础--struct的大小

在修改别人的代码的过程中,发现很多人会把struct和struct的定义混淆,在这里主要是为了提醒自己Struct定义的规范性。

#include <stdio.h>

struct x{
	int a;
	char b;
};

typedef struct g{
	int a;
	char b;
}G;

int main()
{
	int a;
	char b;
	int m = sizeof(a);
	int n = sizeof(b);
	printf("size of int is %d\n", m);
	printf("size of char is %d\n", n);
	int x = sizeof(x);
	int y = sizeof(G);
	printf("size of struct is %d\n", x);
	printf("size of struct G is %d\n", y);
	return 0;
}

  运行的结果为:

在这里Struct G的大小为8是因为:字节对齐,说明Struct在默认情况下,就已经做了字节对齐。

 

posted on 2018-05-24 16:12  Anlia  阅读(698)  评论(1编辑  收藏  举报

导航