Path: utzoo!attcan!uunet!lll-winken!ames!pasteur!ucbvax!tut.cis.ohio-state.edu!bloom-beacon!mit-eddie!rutgers!att!ulysses!andante!alice!debra From: debra@alice.UUCP (Paul De Bra) Newsgroups: comp.unix.questions Subject: Re: Illegal union question Message-ID: <8968@alice.UUCP> Date: 25 Feb 89 20:26:09 GMT References: <833@wzlr.UUCP> Reply-To: debra@alice.UUCP () Organization: AT&T, Bell Labs Lines: 37 In article <833@wzlr.UUCP> jms@wzlr.UUCP (Jack Stephens) writes: > >The following declaration yeilds an "illegal member use" error >for both f and next under the compilers on both by PC and my 3B1 >and I can't seem to understand why. Amybody feel pedegogical ? > >jms > >typedef struct m_i { > int type; > union { > int (*f)(); > struct m_i *next; > } >} menu[10]; Aha, different compilers have a different opinion on this one. First of all, whether the definition is correct or not, the union needs a name, thus union { ... } name_of_union; This is a nuisance because one must access fields like mymenu[5].name_of_union.next instead of mymenu[5].next However, one can have several unions inside the same struct, and such a union may have fields with names that also appear in some other union. Some compilers accept your declaration, some others want at least a ';' after the } that ends the union, but in either case there is no way to access the fields of the union by name. Paul. -- ------------------------------------------------------ |debra@research.att.com | uunet!research!debra | ------------------------------------------------------