enums

Fraser Orr orr at cs.glasgow.ac.uk
Fri Jul 29 22:31:41 AEST 1988


In article <31416 at cca.CCA.COM> alex at CCA.CCA.COM (Alexis Layton) writes:
>
>Actually, it may be better to restrict int->enum; so that "a = 6" above
>would fail without explicit cast.

I agree. In fact, if you have any pretentions to having a reasonable type
system, then this MUST be the case.

>would fail without explicit cast.  But enum->int must be allowed (which
>the pcc does not), so you can have
>
>	char *baker_names[] = { "Red", "Green" };
>
>and say
>
>	printf("%s\n", enum_names[b]);
>

Alternatively, you could have a decleration syntax, that allowed this:

	char * baker_names [ enum baker ] = {"Red", "Green" } ;

and then 

	printf("%s\n", baker_names[b]); /* Legal */
	printf("%s\n", baker_names[i]); /* Illegal */
	printf("%s\n", baker_names[a]); /* Illegal */

would be completely type secure, and the purpose of baker_names would
be much more apparent to the reader.

A previous poster ( sorry I've lost the article) asked how you could find
the number of elements in an enum. Can anyone tellme why such information
would be useful (appart from in array size declerations)?

==Fraser Orr ( Dept C.S., Univ. Glasgow, Glasgow, G12 8QQ, UK)
UseNet: {uk}!cs.glasgow.ac.uk!orr       JANET: orr at uk.ac.glasgow.cs
ARPANet(preferred xAtlantic): orr%cs.glasgow.ac.uk at nss.cs.ucl.ac.uk



More information about the Comp.lang.c mailing list