Good Morning, I am trying to come up with a data structure which can be used in different applications, yet passed in to a transmit function as the same type, I am using netbeans at the moment but this will be transferred on to a dspic30f (16-bit),
typedef union {
union {
struct {
unsigned bit0 : 1;
unsigned bit1 : 1;
unsigned bit2 : 1;
unsigned bit3 : 1;
unsigned bit4 : 1;
unsigned bit5 : 1;
unsigned bit6 : 1;
unsigned bit7 : 1;
unsigned bit8 : 1;
unsigned bit9 : 1;
unsigned bit10 : 1;
unsigned bit11 : 1;
union {
struct {
unsigned bit12 : 1;
unsigned bit13 : 1;
unsigned bit14 : 1;
unsigned bit15 : 1;
};
unsigned char value;
} lastfour;
};
unsigned int value : 16;
};
union {
struct {
union {
struct {
unsigned bit0 : 1;
unsigned bit1 : 1;
unsigned bit2 : 1;
unsigned bit3 : 1;
};
unsigned char value;
} firstfour;
unsigned bit4 : 1;
unsigned bit5 : 1;
unsigned bit6 : 1;
unsigned bit7 : 1;
unsigned bit8 : 1;
unsigned bit9 : 1;
unsigned bit10 : 1;
unsigned bit11 : 1;
unsigned bit12 : 1;
unsigned bit13 : 1;
unsigned bit14 : 1;
unsigned bit15 : 1;
};
unsigned int value : 16;
};
} foo;
I then use the following code to check the functionality.
int main(int argc, char** argv) {
foo a;
a.value =0;
a.lastfour.value = 0xF;
printf("%d", a.value);
return (EXIT_SUCCESS);
}
The printed value is 0, however because of the union I am under the impression the two structure share the same memory (16 bits) so after setting 'lastfour' to 0xF 'value' should now be 0xF000.
Could anyone give some guidance on what I am doing wrong and why 'value' is not reading the same memory which contains 'lastfour'