I'm packing several values from a struct into a buffer to send them over Bluetooth.
I don't want to send the whole struct and I also don't want to deal with padding, so I'm copying just the values I need. I also don't want to use anything that requires external tooling like Protobuf.
This is what I came up with. (Shortened to 6 values, the real code has a couple more.)
char buf[6 + 6 + 8 + 8 + 1 + 1];
size_t len;
size_t offset = 0;
memcpy(buf + offset, &g_system_status.gps_date, len = 6); offset += len;
memcpy(buf + offset, &g_system_status.gps_time, len = 6); offset += len;
memcpy(buf + offset, &g_system_status.gps_start_time, len = 8); offset += len;
memcpy(buf + offset, &g_system_status.gps_fix_time, len = 8); offset += len;
memcpy(buf + offset, &g_system_status.modem_creg, len = 1); offset += len;
memcpy(buf + offset, &g_system_status.modem_cgreg, len = 1); offset += len;
To a C developer (I just dabble) is it sufficiently clear what is happening here or is there a better way?