结构体的大小
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct A
{
char a;
int b;
}
int main()
{
printf("%d\n",sizeof(struct A));
}
结构体成员存放的地址要能整除该成员本身的大小。
1.前面所有成员的大小相加应该是当前成员大小的倍数。
补充char 1个字节;short 2个字节;int 4个字节;long 4个字节;float 4个字节;long long 8个字节;double 8个字节
(1)
struct A
{
char a;//1+3
int b; //4
}; //8
(2)
struct B
{
char a; //1+1
short b;//2
int c;//4
};//8
(3)
struct C
{
int a; //4
char b;//1+1
short c;//2
};//8
2.整个结构体的大小应该是该结构体中单个最大结构体的倍数