I am currently trying to find the simplest way to convert a struct which contains a char * to a char array for transmitting a serial data frame.
The struct stores the frame headers, checksum, msg size etc along with the data to be transmit. eg
struct {
unsigned char header;
unsigned char msgSizeL;
unsigned char msgSizeH;
unsigned char *data;
unsigned char checksum;
....
....
....
....
unsinged char endFrame;
} Frame;
The data is a pointer as the amount of data can change with each frame transmit in the range from 1 to 16 bytes.
I am trying to find the simplest way to convert the struct to an array of unsigned chars without copying out each entry in the struct to the array but don't seem to be able to find a way.
Alternately I don't have to use a struct, as i am generating the frame contents excluding the data contents from scratch, I just want to avoid a case of -
unsigned char frame[1000];
frame[0] = 0x01; // header
frame[1] = msgSizeL; // msg size low byte
frame[2] = msgSizeH; // msg size high byte
frame[3] = data[0]; // data contents 0 Over simplification
frame[4] = data[1]; // data contents 1 for data copying -
frame[5] = data[2]; // data contents 2 data length is variable.
frame[6] = data[3]; // data contents 3
frame[7] = data[4]; // data contents 4
frame[8] = data[5]; // data contents 5
frame[9] = data[6]; // data contents 6
frame[10] = data[7]; // data contents 7
frame[3 + msgSize]; = getChecksum(data);
....
....
....
....
frame [3 + msgSize + 20] = 0xFF; // end frame
where i write data to what reads as a random array index rather than something meaningful like frame.checksum = getChecksum(data);
.
Open to suggestions and a little bit of flaming for asking a general question rather than a specific one :-)