C语言代码片段总结

##
#pragma为C语言预处理宏。可用#pragma pack(N)指定结构体N字节对齐。
1字节对齐:

1
2
3
4
5
6
7
8
9
10
11
#pragma pack(push)
#pragma pack(1)
// 头部
struct header_t {
uint8_t word1;
uint8_t word2;
uint8_t cmd;
uint16_t len;
};

#pragma pack(pop)

GCC编译器扩展形式,1字节对齐:

1
2
3
4
5
6
7
// 头部
struct header_t {
uint8_t word1;
uint8_t word2;
uint8_t cmd;
uint16_t len;
} __attribute__((packed));

  • 本文作者:李迟
  • 版权声明:原创文章,版权归署名作者,转载建议注明出处(当然不注明亦可)。
  • 本文链接:/my-library/c-code-slice.html