其实很简单:
typedef struct
{
int a;
char b;
}__attribute__((packed)) data_t;
然后使用 data_t 进行定义结构体变量即可:
例如 :
data_t a;
printf("%d\n",sizeof(a));
结果为:5
#include<stdio.h>
typedef struct
{
int a;
char b;
}__attribute__((packed)) data_t;
int main(){
data_t a;
printf("%d\n",sizeof(a));
return 0;
}
但是如果调换一下位置:
例如:
是不是很奇怪,有知道原因的么? ^_^