Question about use of enum in typedef and struct

Barnacle Wes wes at obie.UUCP
Mon May 2 10:09:48 AEST 1988


In article <823 at uvm-gen.UUCP>, cavrak at uvm-gen.UUCP (Steve Cavrak) writes:
| Can someone explain why the following uses (abuses?) of typdef and enum
|
|a) typedef int s_state_ok;				/*  OK		*/
|b) typedef enum s_state_ok {
| 	initial_ok, middle_ok, final_ok
| 	};
|  
|c) typedef int s_state_to;				/*  OK		*/
|d) enum s_state_to {
| 	initial_to, middle_to, final_to
| 	};
|  
|e) typedef enum s_state_no  {				/*  NOT OK	*/
| 	initial_no, middle_no, final_no
| 	};

Your problem lies mainly with your typedef and enum syntax.  Remember
that the name following the keyword `enum' is an _enumeration type
tag_ (like a structure tag), not a variable declaration.  The syntax
for typedef is to write the keyword typedef, followed by a valid C
variable declaration.  The name that would normally become the
variable becomes the new type name.

The enum statement looks like:
	enum enum_type_tag { element, [elements...] } variable;

The typedef statement looks like:
	type valid_c_variable_declaration;
where the variable becomes the new type name.

What your code above is actually doing is declaring:
a) a new type, s_state_ok, which is int.
b) a new enumeration type, which is called s_state_ok (but no
variables of this type are declared).
c) a new type, s_state_to, which is int.
d) a new enumeration type, which is called s_state_to (again no
variables of this type are declared).
e) a non-working typedef - no variable was included in the 'enum...'
statement, so no type name could be found.

I think what you may want is:

typedef enum { initial_ok, middle_ok, final_ok } s_state_ok ;
typedef enum { initial_to, middle_to, final_to } s_state_to ;
typedef enum { initial_no, middle_no, final_no } s_state_no ;

-- 
    /\              -  "Against Stupidity,  -    {backbones}!
   /\/\  .    /\    -  The Gods Themselves  -  utah-cs!uplherc!
  /    \/ \/\/  \   -   Contend in Vain."   -   sp7040!obie!
 / U i n T e c h \  -       Schiller        -        wes



More information about the Comp.lang.c mailing list