Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!iuvax!rutgers!cmcl2!adm!smoke!gwyn From: gwyn@smoke.BRL.MIL (Doug Gwyn ) Newsgroups: comp.unix.questions Subject: Re: Illegal union question Message-ID: <9724@smoke.BRL.MIL> Date: 26 Feb 89 03:23:01 GMT References: <833@wzlr.UUCP> Reply-To: gwyn@brl.arpa (Doug Gwyn (VLD/VMB) ) Followup-To: comp.lang.c Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 39 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 ? >typedef struct m_i { > int type; > union { > int (*f)(); > struct m_i *next; > } >} menu[10]; This is a C question, not a UNIX question. It's a small miracle that the compiler accepted the declaration, which is missing a semicolon after the first }. The real problem is that you need to name the union member of the structure and use its name as part of the chain of member names to reach the innards of the union. For example: typedef struct m_i { int type; union { int (*f)(); struct m_i *next; } u; } menu[10]; /* ... */ extern menu root_menu, *make_menu(); menu *x = make_menu( &root_menu ); int i = get_selection( x ); while ( (*x)[i].type != 0 ) { x = make_menu( (*x)[i].u.next ); i = get_selection( x ); } (*(*x)[i].u.f)( x, i );