Path: utzoo!mnetor!uunet!seismo!sundc!pitstop!sun!limes From: limes@sun.uucp (Greg Limes) Newsgroups: comp.lang.c Subject: Re: Question about use of enum in typedef and struct Message-ID: <51434@sun.uucp> Date: 30 Apr 88 04:38:41 GMT References: <823@uvm-gen.UUCP> Reply-To: limes@sun.UUCP (Greg Limes) Organization: Sun Microsystems, Mountain View Lines: 60 Keywords: enum, typedef, struct, syntax In article <823@uvm-gen.UUCP> cavrak@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@sun.com] frames to /dev/fb