Hi,
I hope someone can help, I am modifying an EByte WiFi-Uart library for Teensy and it includes various forms of this sort of code "Configuration configuration = *(Configuration*) c.data;".
Can anyone help explain what it means/does.
Thanks in advance.
.
struct Example {
uint8_t a;
uint8_t b;
uint8_t c;
uint8_t d;
};
uint8_t data[4] = {1, 2, 3, 4};
void setup() {
Serial.begin(115200);
while(!Serial) {}
Example ex = *(Example *)data;
Serial.println(ex.a);
Serial.println(ex.b);
Serial.println(ex.c);
Serial.println(ex.d);
}
void loop() {}
Agree. This is a great way to write code that's "accidentally right," so it's best done with care, and only where necessary.
struct Example {
uint8_t a;
uint8_t b;
uint8_t c;
uint8_t d;
};
uint8_t data[4] = {1, 2, 3, 4};
static_assert(sizeof(Example) == sizeof(data), "Size mis-match");
void setup() {
Serial.begin(115200);
while(!Serial) {}
Example ex = *(Example *)data;
Serial.println(ex.a);
Serial.println(ex.b);
Serial.println(ex.c);
Serial.println(ex.d);
}
void loop() {}