I am using the following structures for formatting the data of CAN-messages. messageData.ptr is used for further processing. The section part ist used for better access. The problem is that whyever there are two more bytes between messageData.srcSpecifier and messageData.data
typedef union _MessagePureData_TypeDef
{
signed int S32[1];
unsigned int U32[1];
unsigned short U16[2];
signed short S16[2];
unsigned char U8[4];
} messagePureData;
typedef union _MessageData_TypeDef
{
unsigned char ptr[6];
struct
{
unsigned char srcDevice;
unsigned char srcSpecifier;
messagePureData data;
} section;
} messageData;
example:
messageData.section.srcDevice = 0xAA;
messageData.section.srcSpecifier = 0xBB;
messageData.section.data.U32 = 0x11223344;
results that messageData.ptr contains: [0xAA, 0xBB, 0x01, 0x17, 0x44, 0x33]
so from where is 0x01 and 0x17 ??
Alignment.
A
messagePureDatais always aligned for anint, which is perhaps 4 bytes large. That means thatsectionis most certainly aligned for 8 bytes, putting two padding bytes betweensrcSpecifieranddata(so a whole struct object has an alignment of 8 anddataone of 4). On the other hand,ptras an array is continuous, so it covers the same storage as the two padding bytes.You can pack the struct to circumvent this.