unions inside structures
Chris Torek
chris at umcp-cs.UUCP
Sun Jan 20 10:15:30 AEST 1985
Concerning the problem of ``invented names'':
Suppose you need a structure to hold either a character/font pair, or
an adjustment. Then you might write
enum node_type { ntype_char, ntype_adjust };
struct char_node {
char cn_char;
char cn_font;
};
struct node {
enum node_type n_type; /* is it a char or an adjust? */
union {
struct char_node un_char;/* value if char */
short un_adj; /* value if adjust */
} n_un;
};
(Note the ``invented name'' n_un.)
Then a reasonable compromise for accessing the fields of a struct node
is to create the definitions
#define n_char n_un.un_char
#define n_adj n_un.un_adj
You can then write
put(np)
struct node *np;
{
if (np->n_type == ntype_char) {
select_font(np->n_char.cn_font);
put_char(np->n_char.cn_char);
}
else
do_adj(np->n_adj);
}
which makes the ``n_un.un'' part of each name vanish. Admittedly
this has problems, but it can improve readability---and it requires
no changes to the language.
--
(This line accidently left nonblank.)
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)
UUCP: {seismo,allegra,brl-bmd}!umcp-cs!chris
CSNet: chris at umcp-cs ARPA: chris at maryland
More information about the Comp.lang.c
mailing list