联盟C

时间:2019-02-26 14:16:10

标签: c memory alignment union offset

我在下面的Keil的C语言中遇到问题 这是示例C代码。

struct A
{
    uint8_t header;
    uint16_t volume;
    uint16_t temp;
    uint8_t footer;
};


union {
    struct A Packet;
    uint8_t buff[6];
} A_union;

问题是我想用头文件,页脚和数据填充结构,并用buff对其进行序列化,但是union更改了字节偏移量(我什至看不到buff中的页脚),如图所示。我该怎么解决?

更新:如Sander在this link中建议的那样,我通过如下修改结构来解决了问题

    struct __attribute__((__packed__)) A{
...}

 this is image filled each byte with distinctive value for each byte

0 个答案:

没有答案