Use of enumerations.

ps at celerity.UUCP ps at celerity.UUCP
Tue Apr 17 23:54:32 AEST 1984


I have encountered a problem using enumerations with the 4.2bsd C compiler.
The System 5 C Programming Guide states that the current language treats
enumeration variables and constants as being of *int* type. There are
apparently no special constructs in the language for incrementing and
decrementing enumeration variables, and declaring arrays indexed by
enumeration types. I assumed that the correct way of doing this would be to
use the equivalent integer operations.

However, the attached program causes the following error messages:

"testwk.c", line 18: illegal comparison of enums
"testwk.c", line 18: operands of ++ have incompatible types
"testwk.c", line 19: operands of + have incompatible types
"testwk.c", line 19: illegal indirection

The compiler appears to be complaining about all uses of the enumeration
type other than simple assignment and equality comparison.

Is this how it ought to be? If so, enumerations are unlikely to be very
useful. The following operations seem to be necessary:

	Increment and decrement,
	Compare,
	Array index,
	
These operations are all available, for example, with Pascal scalar types.
I would welcome suggestions for getting the same effects with C enumeration
types, other than the use of excessive casting to integer. Type casting to
integer works, but destroys much of the program clarity that the enumeration
type should have gained.

==========================================================================
main () {
    enum col {
	red,
	    blue,
	    green,
	    black
    };

    enum col colvar;

    static char *colstrings[] = {
	"red",
	"blue",
	"green",
	"black"
    };

    for (colvar = red; colvar <= black; colvar++)
	printf ("%s\n", colstrings[colvar]);
}


    
-- 
	ps
	(Pat Shanahan)
	uucp : {decvax!ucbvax || ihnp4 || philabs}!sdcsvax!celerity!ps
	arpa : sdcsvax!celerity!ps at nosc



More information about the Comp.lang.c mailing list