先算内结构体的大小,在以外数据类型最大的数据大小对齐,然后加上其他数据对齐后的大小。
比如以下代码。
#include <stdio.h>
typedef struct _s
{
int a;
short b;
char buf[11];
long long c;
}ST;
typedef struct _c
{
char num[13];
int x;
struct _s data[3];
}CT;
int main(int argc,char *argv[])
{
printf("ST:%d,CT:%d\n",sizeof(ST),sizeof(CT));
}
本文通过具体示例详细解释了C语言中结构体对齐的原则和计算方法,展示了如何确定结构体成员变量对齐后的实际占用空间。
514

被折叠的 条评论
为什么被折叠?



