I am writing code for an ATSAM device using XC32 from Microchip (not clear to me if C99 or C11).
This code works fine:
typedef union {
struct {
uint16_t bit1 : 1;
uint16_t bit2 : 1;
// .. MUCH more lines...
};
struct {
uint32_t data[N];
// ... more lines, with sub-structures....
};
} Context_t;
Context_t c;
c.data[0] = 1;
c.bit2 = false;
Notice both anonymous structures.
Since both structures are large, change all the time and their code are generated automatically by other script, it would be more convenient to use them like this:
// bit.h
typedef struct {
uint16_t bit1 : 1;
uint16_t bit2 : 1;
// .. MUCH more items...
} Bits_t;
// data.h
typedef struct {
uint32_t data[N];
// ... more code...
} Data_t;
// import both headers
typedef union {
Bits_t;
Data_t;
} Context_t;
Context_t c;
c.data[0] = 1;
c.bit2 = false;
This fails to build with compiler saying declaration does not declare anything for both lines inside union. That sounds fair to me.
If I name them like bellow it works, but final code will be forced to be aware of this change in structure. Not good for our application.
typedef union {
Bits_t b;
Data_t d;
} Context_t;
Context_t c;
c.d.data[0] = 1;
c.b.bit2 = false;
I assume I am the culprit in here and failing example is failing due to me declaring the union the wrong way.
Any tip are welcome!
uint32_tand twouint16_tvalues? For youruint_32values, you could simply cast them to the desired type as needed.