A SERVICE OF

logo

CAVR-4
Part1. Using the compiler
Efficient coding for embedded applications
129
Example
In the following example, the members in the anonymous
union can be accessed, in
function
f, without explicitly specifying the union name:
struct s
{
char tag;
union
{
long l;
float f;
};
} st;
void f(void)
{
st.l = 5;
}
The member names must be unique in the surrounding scope. Having an anonymous
struct or union at file scope, as a global, external, or static variable is also allowed.
This could for instance be used for declaring I/O registers, as in the following example:
__no_init volatile
union
{
unsigned char IOPORT;
struct
{
unsigned char way: 1;
unsigned char out: 1;
};
} @ 0x1234;
This declares an I/O register byte IOPORT at the address 0x1234. The I/O register has 2
bits declared,
way and out. Note that both the inner structure and the outer union are
anonymous.
The following example illustrates how variables declared this way can be used:
void test(void)
{
IOPORT = 0;
way = 1;
out = 1;
}