Struct 结构比预期大一个字节
我在下面的工会有一个工会Struct 结构比预期大一个字节,struct,unions,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 deci
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 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);
当“消息”位于0x20000490时,字节将写入位置0x20000498。如果计数正确,我希望字节为0x20000497
它为什么要在那个位置写呢?简单的回答是,编译器选择那个位置,它有这样做的自由。你为什么要在意呢?因为我通过无线电发送的数据包是payloaddata+头长的,现在我错过了最后一个字节,因为有一个字节没有使用。有没有办法强迫编译器按照我的要求去做?