We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Good moorning,I am trying to create a struct of date of this style://Struct 1
union{ struct{ union{ struct{ bool interrupt_receive; bool fifo_mode; bool enable; bool bits_14_mode; bool int_source_readed; bool void6; bool void7; bool void8; }; unsigned char registro; }REG_CTRL_FLG; union{ struct{ unsigned int REG_CTRL_TIMEH; unsigned int REG_CTRL_TIMEL; }; unsigned short REG_CTRL_TIME; }; unsigned int threshold; //Para pruebas pendiente de eliminar //AXIS_STRUCT data_axis; //AXIS_EXTEND_STRUCT data_full_axis; }registros_acelerometro; unsigned int registro[3]; }ACELEROMETRO;
//Struct 2
typedef union{ struct{ unsigned char dlc_0; union{ struct{ unsigned char p15:1; unsigned char p14:1; unsigned char p13:1; unsigned char p12:1; unsigned char p11:1; unsigned char p10:1; unsigned char p9:1; unsigned char p8:1; unsigned char p7:1; unsigned char p6:1; unsigned char p5:1; unsigned char p4:1; unsigned char p3:1; unsigned char p2:1; unsigned char p1:1; unsigned char p0:1; }; unsigned short p; }pulsadores; union{ struct{ unsigned char lb0:1; unsigned char lb1:1; unsigned char ls0:1; unsigned char ls1:1; unsigned char iluminacion:1; unsigned char completo:1; unsigned char sobrecarga:1; unsigned char void7:1; }registro_cabina; unsigned char estado_cabina; }; unsigned char dlc_4; }registros_can_data; unsigned char can_data[5]; }TRESA_PULS;
The unions have nothing to do with that. You're overlooking the need for alignment of integer types larger than a single byte.
Thanks for your answer.I'm not seeing itIf it's not too much trouble, could you please give me an example?
The alignment of the union pulsadores is set by the alignment requirement of its member 'p'. The compiler inserts a padding byte between dlc_0 and pulsadores. You can tell it not to using type attributes __attribute__((packed)) or __packed, depending on your compiler. There are many examples of use in the compiler books included with the Keil IDE.