I have an unsigned char* (bytes sent from the server) that I need to convert/cast to an array of structs whose size exactly matches that of the unsigned char*. Without copying values, how do I achieve this? In the uchar*, there could be any number of structs.
How do I achieve this?
I have tried the following (psuedo code):
// Define the struct:
struct MyStruct:
unsigned char name[6]
long time
double speed
// ...some code...
// Get the uchar*.
unsigned char* chars = getUchars()
// The uchar*'s first byte is a special flag,
// so the structs start at index 1 not 0.
unsigned char* chars2 = &chars[1]
// Convert chars2 to an array of MyStructs.
MyStruct *structs = (MyStruct*) chars2
// Get the first struct and print values.
MyStruct s1 = structs[0]
_print(charArrayToString(s1.name), s1.time, s1.speed)
// When this _print() is called, the struct's
// name value is printed correctly, but its time
// and speed are not printed correctly.
// Get the second struct.
MyStruct s2 = structs[1]
_print(charArrayToString(s2.name), s2.time, s2.speed)
// None of this second struct's name, time and speed
// is printed correctly.
Where did I make mistakes?
longanddoublehave alignment requirements.chars2is probably not aligned correctly for them.