Question about use of enum in typedef and struct

Greg Limes limes at sun.uucp
Sat Apr 30 14:38:41 AEST 1988


In article <823 at uvm-gen.UUCP> cavrak at uvm-gen.UUCP (Steve Cavrak) writes:
>/*  The "_ok" declaration is usable in the struct definition AND	*/
>/*  the "_to" declaration is usable, BUT				*/
>/*  the "_no" declaration generates a syntax error.			*/
>typedef int s_state_ok;				/*  OK		*/
>typedef enum s_state_ok {
>	initial_ok, middle_ok, final_ok
>	};
> 
>typedef int s_state_to;				/*  OK		*/
>enum s_state_to {
>	initial_to, middle_to, final_to
>	};
> 
>typedef enum s_state_no  {				/*  NOT OK	*/
>	initial_no, middle_no, final_no
>	};
> 
>struct	{
>	int m;
>	int n;
>	s_state_ok s;		/* s_state_ok works, s_state_no fails 	*/
>	} machine_state;

Small change. The identifier between "enum" and "{" is only an
enumerator tag, not a type definition; since enumeration tags and type
definitions can never occur in the same place, the compiler can keep
separate lists and search only the proper list.  note that the only
difference between "s_state_ok" and "s_state_no" is whether they appear
as the target of a "typedef" statement. To make it all a bit more
standard, you might consider making the types analogous to the
enumeration tags, as follows:

	typedef enum s_state_ok {
	    initial_ok, middle_ok, final_ok 
	} s_state_ok;

	typedef enum s_state_to { 
	    initial_to, middle_to, final_to 
	} s_state_to;

	typedef enum s_state_no {
	    initial_no, middle_no, final_no 
	} s_state_no;

	struct {
	    int m;
	    int n;
	    s_state_no s;
	} machine_state;

Strictly speaking, the enumeration tags are not needed, unless you want
to later use them like
	enum s_state_no	foo;
which could in fact now be written as
	s_state_no foo;

Hope this clears things up a bit.
-- 
   Greg Limes [limes at sun.com]				frames to /dev/fb



More information about the Comp.lang.c mailing list