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
Post a Comment