struct packet_format {
uint32_t field_a;
uint8_t field_b;
uint16_t field_c;
uint32_t field_d;
uint16_t field_e;
uint8_t field_f;
};
uint32_t size_of_packet = 14;
uint8_t *data_to_transmit = malloc(size_of_packet);
struct packet_format *s = (struct packet_format *)data;
s->field_a = 1;
s->field_b = 2;
s->field_c = 3;
s->field_d = 4;
s->field_e = 5;
s->field_f = 6;
sendto(... data_to_transmit, size_of_packet ...)
Due to structure padding, the data transmitted (data_to_transmit) is not guaranteed to be correct (in the format of the structure declared), right? So, what is the best way to put data in data_to_transmit in the right format? Will I need to memcpy every field of the structure separated?
Edit: little mistake on title