结构体的大小

#include <stdio.h>

typedef struct{
	int item[100];
	int last;
}indexList;

int main()
{
	indexList list;
	list.item[2] = 100;
	printf("sizeof(indexList) = %d,item[2] = %d\n",sizeof(list),list.item[2]);
	return 1;
}

上述结构体list的大小为:sizeof( int )*100+sizeof(int) = 4 * 100 + 4 = 404。假如将item的类型定义为一个结构体类型,如下:

#include <stdio.h>

typedef struct{
	int x;
	int y;
}Item;

typedef struct{
	Item item[100];
	int last;
}indexList;

int main()
{
	indexList list;
	printf("sizeof(indexList) = %d\n",sizeof(list));
	return 1;
}

那么此刻list的大小为:sizeof( Item )*100 + sizeof( int ) = 8 * 100 + 4 = 804

posted @ 2019-08-20 21:12  昨夜昙花  阅读(9)  评论(0)    收藏  举报