# 一个牛X的例子来计算结构体的sizeof

0 4 8
0 4 8
0 4 6
sizeof(E) = 16
sizeof(F) = 16
sizeof(T) = 14
sizeof(struct_taga) = 4
sizeof(struct_tagb) = 3
sizeof(struct_tagc) = 4

--------------
#include <stddef.h>
#include <stdio.h>

//package size defaults to 8
struct E
{
int i;   // size 4
short j;   // size 2
double k;   // size 8
};

#pragma pack(4)
struct F
{
int i;
short j;
double k;
};

#pragma pack(2)
struct T
{
int i;
short j;
double k;
};

struct struct_taga
{
char i;
short j;
};

#pragma pack(push, r2, 1)
struct struct_tagb
{
char i;
short j;
};

#pragma pack(push, 8)
struct struct_tagc
{
char i;
short j;
};

void main()
{
printf("%d ", offsetof(E, i));
printf("%d ", offsetof(E, j));
printf("%d\n", offsetof(E, k));
printf("%d ", offsetof(F, i));
printf("%d ", offsetof(F, j));
printf("%d\n", offsetof(F, k));
printf("%d ", offsetof(T, i));
printf("%d ", offsetof(T, j));
printf("%d\n", offsetof(T, k));

printf("sizeof(E) = %d\n", sizeof(E));
printf("sizeof(F) = %d\n", sizeof(F));
printf("sizeof(T) = %d\n", sizeof(T));

printf("sizeof(struct_taga) = %d\n", sizeof(struct_taga));
printf("sizeof(struct_tagb) = %d\n", sizeof(struct_tagb));
printf("sizeof(struct_tagc) = %d\n", sizeof(struct_tagc));
}
• 本文已收录于以下专栏：

举报原因： 您举报文章：一个牛X的例子来计算结构体的sizeof 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)