Can I cast a pointer to a double as type char *, then use that pointer to break the double into bytes?
Here’s example code:
double data;
double *dblPoint = &data;
unsigned char *bytePoint = (unsigned char *)dblPoint;
unsigned char byteArray[sizeof (double)];
unsigned int i;
for(i = 0; i < sizeof(double); i++) {
byteArray[i] = *(bytePoint + i);
}
byteArray is then transmitted via UART to another computer and reconstructed (bytesReceived contains the incoming data):
unsigned char bytesReceived[sizeof (double)];
double reconstData;
double *newDblPoint;
unsigned int i;
newDblPoint = bytesReceived;
reconstData = *dblPoint;
So, after all that, would data == reconstData?
Your first example can be simplified to:
Your second example is not valid: the buffer
Is not necessarily aligned on a “double” boundary, so that the cast
will attempt to dereference an unaligned pointer.