c - Byte array unioned with a struct (byte aligment) -


i combine struct , byte array in union. compiler gcc.

is following considered good/save code 32bit embedded controller (avr)? have worry byte aligment?

#include <stdint.h>  typedef int8_t s8;  typedef union {     struct {         s8 a;         s8 b;         s8 c;         s8 d;         s8 e;     };     s8 array[5]; } s_t; 

initialization:

s_t s = {.array = {0, 0, 0, 0, 0}}; 

access:

s.a = 50; s.c = 42; 

i think you're showing fine, should worried if ever use array of s_t there might padding @ end.

you can tell gcc "pack" struct using extension __attribute__ syntax. add __attribute__((packed)) before final ;.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -