Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site watmath.UUCP Path: utzoo!watmath!kpmartin From: kpmartin@watmath.UUCP (Kevin Martin) Newsgroups: net.lang.c Subject: Re: unions inside structures Message-ID: <11085@watmath.UUCP> Date: Tue, 22-Jan-85 15:22:37 EST Article-I.D.: watmath.11085 Posted: Tue Jan 22 15:22:37 1985 Date-Received: Wed, 23-Jan-85 05:09:39 EST References: <133@ISM780B.UUCP> <10975@watmath.UUCP> <2606@umcp-cs.UUCP> Reply-To: kpmartin@watmath.UUCP (Kevin Martin) Organization: U of Waterloo, Ontario Lines: 41 Summary: In article <2606@umcp-cs.UUCP> chris@umcp-cs.UUCP (Chris Torek) writes: >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; > }; > >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 > >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. The problem with this is that the fake element names 'n_char' and 'n_adj' have a scope which differs from that of any other struct elements. That is, the element names 'n_type', 'n_un', 'un_adj', etc. are only recognized when preceded by 'primary.' or 'pointer->', where the primary or pointer is of the appropriate type. On the other hand, 'n_char' and 'n_adj' cannot be used in any *other* context (unless you happen to have a variable called 'n_un' which happens to be a struct or union...). The other problem is that a symbolic debugger would have no idea what p->n_adj means. Kevin Martin, UofW Software Development Group