#include <stdio.h> struct s1{ int a; char b[6]; char c[6]; char d[2]; }; struct s2{ char b[6]; char c[6]; char d[2]; }; int main() { printf("the result is:%d %d %d",sizeof(int),sizeof(struct s2), sizeof(struct s1)); }
the result is 4,14,20. 在没有int的情况下,原来多大就多大. 在有int的情况下: 1.int在所有的char之前 a.若后面所有的char长度之和mod 4=0,则原来多大就多大. b.若其之和mod 4!=0;则它会把该和变成mod 4=0; 例如: sturct s1{ int i; char ch0[1]; char ch1[5]; };的大小是12,1+5=6,6改成8; struct s1{ int i; char ch0[1]; char ch1[5]; char ch2[2]; };的大小是12;1+5+2=8,mod 4=0; 2.若int在所有的char之后 如同int在所有的char之前. 3.若是char与int混杂,则可以如前分开来算. 例如: struct s1{ char ch0[1];//1变成4; int i0;//4 char ch1[5]; char ch2[2];//5+2=7;变成8; int i1;//4 char ch3[2];//2 变成4; int i2; //4 };.//大小为4+4+8+4+4+4=28;