结构体:不同类型元素的集合
结构体对齐:要保证数据传送的完整性,否则会影响效率。
对齐原则:宏观微观都以最大类型所占字节为标准进行对齐。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct AA
{
int a;
char b;
short c;
}BB;
int main()
{
BB a;
printf("%d\n",sizeof(a));//8
return 0;
}
以最大的int类型所占字节为基准
a | a | a | a |
---|---|---|---|
b | c | c |
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct AA
{
char a;
int b;
short c;
}BB;
int main()
{
BB a;
printf("%d\n",sizeof(a));//12
return 0;
}
以最大的int类型所占字节为基准
a | |||
---|---|---|---|
b | b | b | b |
c | c |