The struct members are being aligned to units of their size. So the struct has a 2-byte gap between unsigned char brevision
and unsigned int iPageNumber
, so that iPageNumber
starts at a 4-byte boundary. I'm not aware of a standards-compliant way to remove that padding, but for GCC you can specify __attribute__((packed))
:
typedef struct __attribute__((packed)) {
unsigned int ibaseRecord;
unsigned int irecordNumber;
unsigned char brecordType;
unsigned char brevision;
unsigned int ipageNumber;
unsigned int ireserve1;
unsigned int ireserve2;
unsigned int ireserve3;
unsigned short scrc16;
} DATABASEPAGEHEADER_TypeDef;
If you want to make this properly portable though; don't use that attribute, or memcpy at all. Instead, initialize your struct fields individually from the byte data, and consider using specifically sized data types like uint32_t
instead of unsigned int
.