I have a code that is very similar to the following
struct T {
union {
unsigned int x;
struct {
unsigned short xhigh;
unsigned short xlow;
};
} x;
};
This does exactly what you expect: it allows me to declare a type variable struct Tand access t.x.xeither t.x.xhighor or t.x.xlow. So far so good.
Nevertheless, I would very much like if I could only dot.x in the general case, to gain access to the value of the union as a quantity unsigned int, but to retain the ability to access high and low order sections independently, without resorting to masking and bit shifting, and not causing undefined behavior.
Is this possible in C?
If possible, what is the C syntax for the declaration?
naiive- t.x t.x.x, ( printf()):
cc -ansi -o test -Wall test.c
test.c: In function ‘my_function’:
test.c:13:2: warning: format ‘%X’ expects argument of type ‘unsigned int’, but argument 2 has type ‘const union <anonymous>’ [-Wformat]
-std=c11 -ansi .