I have the following structure:
typdef struct {
char a[4];
int b;
char c;
int d;
} varT; // Total size is 13 bytes, without padding
which shows the desired memory layout in RAM for variables of type 'varT';
Which means that in RAM, the following layout of data is required:
From address 1000 to 1013 (excluding 1013):
[ field 'a', 4 bytes | field 'b', 4 bytes | field 'c', 1 byte | field 'd', 4 bytes ]
From address 1013 to 1026 (excluding 1026):
[ field 'a', 4 bytes | field 'b', 4 bytes | field 'c', 1 byte | field 'd', 4 bytes ], ...
But the compiler adds padding of 3 bytes between field c
to field d
.
How would you recommend to bypass that padding in order to conveniently deal with this type of variable, that is stored in RAM?
What I'd like to do is similar to the following:
varT var;
varT *list[5];
var.a[0] = 'A'; var.a[1] = 'B'; var.a[2] = 'C'; var.a[3] = 'D';
var.b = 9876;
var.c = 'X';
var.d = 1234;
memcpy((void*)(list), (void*)(&var), 13);
var.a[0] = 'E'; var.a[1] = 'F'; var.a[2] = 'G'; var.a[3] = 'H';
var.b = 6543;
var.c = 'V';
var.d = 8887;
memcpy((void*)(list + 13), (void*)(&var), 13);
But this is not possible due to the padding that the compiler adds to the struct.