Mikroelektronika MIKROE-738 Datenbogen
mikroC PRO for PIC32
MikroElektronika
217
Anonymous Unions
Anonymous unions are unions that are declared without
tag
or
declarator
:
union { member-declarator-list };
Such union declarations do not declare
types
; they declare an unnamed
objects
.
The name of each union member must be unique within the scope where the union is declared.
In C, an anonymous union can have a tag; it cannot have declarators. Names declared in an anonymous union are
used directly, like nonmember variables.
In addition to the restrictions listed above in Union, anonymous unions are subject to additional restrictions:
- They must also be declared as
static
if declared in global scope. If declared in local scope, they must be
either
static
or
automatic
, not
external
- They can have only public members; private and protected members in anonymous unions generate
errors.
- They cannot have function members.
Here is a simple example:
union { // no union tag
int i;
float f;
union { // no union tag
unsigned char uc;
char c;
}; // no declarator
}; // no declarator
Anonymous Union Member Access
Anonymous union members are accessed directly because they are in the scope containing the anonymous union:
// Referring to declarations from the example above:
i = 1;
f = 3.14;
uc = ‘c’;
c = ‘u’;