I have such struct
struct hour_parameters{
uint8_t VALUE_00;
uint8_t VALUE_01;
uint8_t VALUE_02;
uint8_t VALUE_03;
uint8_t VALUE_04;
uint8_t VALUE_05;
uint8_t VALUE_06;
uint8_t VALUE_07;
uint8_t VALUE_08;
uint8_t VALUE_09;
uint8_t VALUE_10;
uint8_t VALUE_11;
uint8_t VALUE_12;
uint8_t VALUE_13;
uint8_t VALUE_14;
uint8_t VALUE_15;
uint8_t VALUE_16;
uint8_t VALUE_17;
uint8_t VALUE_18;
uint8_t VALUE_19;
uint8_t VALUE_20;
uint8_t VALUE_21;
uint8_t VALUE_22;
uint8_t VALUE_23;
};
struct hour_parameters hparam;
I would like to assign an uint8_t x[24] hparam, how can I do it with a for loop, that
hparam.value00 = x[0];
hparam.value01 = x[1];
and so on?
You really should use an array in your struct but....
#include <string.h>
memcpy(&hparam, x, sizeof(hparam));
( I'm ducking under the table now )
One the reasons this is dangerous is possible padding in the struct. Now, since they are all bytes, you are pretty safe. But, technically, this kind of stuff is not legal. One thing you could do before hand is
assert(sizeof(hparam) == sizeof(x));
If you insist on a for loop:
for(int i = 0; i != sizeof(hparam); i++) {
((uint8_t *)&hparam)[i] = x[i];
}
Which is ugly and not too kosher either. Kerrek's comments below present reasons not to do this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With