#include <stdio.h>
#include <stdint.h>
struct {
char a;
short b;
int c;
}buf;
int main(void) {
printf("%d",sizeof(buf));
return 0;
}
结果为8,因为有效对齐值N是最终用来决定数据存放地址方式的值。有效对齐N表示“对齐在N上”,即该数据的“存放起始地址%N=0”。而数据结构中的数据变量都是按定义的先后顺序存放。第一个数据变量的起始地址就是数据结构的起始地址。结构体的成员变量要对齐存放,结构体本身也要根据自身的有效对齐值圆整(即结构体成员变量占用总长度为结构体有效对齐值的整数倍)。
C语言字节对齐问题详解 - clover_toeic - 博客园
2.
#include <stdio.h>
#include <string.h>
//#define
union seat{
struct {
unsigned char type; //0x01:ZIGBEE 振动传感器
unsigned char channel; //传感器通道号(该字段作为Zigbee的本机地址)(默认1)
unsigned short panId; //Zig