Illegal union question

Doug Gwyn gwyn at smoke.BRL.MIL
Sun Feb 26 13:23:01 AEST 1989


In article <833 at wzlr.UUCP> jms at 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 );



More information about the Comp.unix.questions mailing list