结构比预期大一个字节

时间:2018-06-25 20:04:06

标签: struct unions

我有一个以下工会

typedef union {
    uint8_t     bValue;
    uint16_t    uiValue;
    int16_t     iValue;
    uint32_t    ulValue;
    int32_t     lValue;
    struct { // Float messages
        float   fValue;
        uint8_t fPrecision; //Number of decimals when serializing
    };
    struct { //Presentation messages
        uint8_t version;        // Library version
        uint8_t sensorType;     // Sensor type hint for controller, see table above
    };
    char        data[MAX_PAYLOAD + 1];
} mysensor_payload_data;

我想在以下typedef中使用此联合

typedef union {
    struct {

#endif
    uint8_t last;                // 8 bit - Id of last node this message passed
    uint8_t sender;              // 8 bit - Id of sender node (origin)
    uint8_t destination;         // 8 bit - Id of destination node
    uint8_t version_length;      // 2 bit - Protocol version
                                 // 1 bit - Signed flag
                                 // 5 bit - Length of payload
    uint8_t command_ack_payload; // 3 bit - Command type
                                 // 1 bit - Request an ack - Indicator that receiver should send an ack back.
                                 // 1 bit - Is ack messsage - Indicator that this is the actual ack message.
                                 // 3 bit - Payload data type
    uint8_t type;                // 8 bit - Type varies depending on command
    uint8_t sensor;              // 8 bit - Id of sensor that this message concerns.
    // Each message can transfer a payload. We add one extra byte for string
    // terminator \0 to be "printable" this is not transferred OTA
    // This union is used to simplify the construction of the binary data types transferred.
    mysensor_payload_data   payload_data;
};
uint8_t array[HEADER_SIZE + MAX_PAYLOAD + 1];
} /*__attribute__((packed))*/ MyMessage;

现在,当我运行以下代码行

strncpy(message->payload_data.data, value, length); 

当“消息”为0x2000490时,字节写入位置0x20000498。如果我计数正确,我希望字节为0x20000497。

为什么要在那个位置写?

0 个答案:

没有答案