mikroC
making it simple...
Unions
Union types are derived types sharing many of the syntactic and functional fea-
tures of structure types. The key difference is that a union allows only one of its
members to be "active" at any given time, the most recently changed member.
Note: mikroC does not support anonymous unions (ANSI divergence).
Union Declaration
Unions are declared same as structures, with the keyword
struct
union tag { member-declarator-list };
Unlike structures' members, the value of only one of union's members can be
stored at any time. Let's have a simple example:
union myunion {
int i;
double d;
char ch;
} mu, *pm = μ
The identifier
4-byte
Size of Union
The size of a union is the size of its largest member. In our previous example, both
sizeof(union myunion)
(padded) when
char
Union Member Access
Union members can be accessed with the structure member selectors (
but care is needed. Check the example on the following page.
MikroElektronika: Development tools - Books - Compilers
:
// union tag is 'myunion'
, of type
mu
union myunion
, or a single-byte
double
holds an
mu
int
.
mikroC - C Compiler for Microchip PIC microcontrollers
, can be used to hold a 2-byte
, but only one of these at any given time.
char
and
return 4, but 2 bytes are unused
sizeof(mu)
object, and 3 bytes are unused when
used instead of
union
, a
int
holds a
mu
and
),
.
->
page
79
Need help?
Do you have a question about the PIC Microcontrollers PIC12 and is the answer not in the manual?