Number of enums

Dave P. Schaumann dave at cs.arizona.edu
Tue Jan 22 10:51:00 AEST 1991


In article <1991Jan21.133917.18458 at cbnews.att.com> jbr at cblph.att.com writes:
|In article <703 at caslon.cs.arizona.edu| dave at cs.arizona.edu (Dave P. Schaumann)
|writes:
||C is lacking (as far as I know) a general way to get the last value of an
||enum type.  Having this would aid in the use of enums in two ways:
||[...]
|
|Good point.  Here is what I do:
|
|typedef enum {
|	Thing1 = 0,
|	Thing2,
|	Thing3,
|	  ...
|	NumThings	/* this must always be last */
|} ThingList;
|
|While some may argue that "NumThings" doesn't belong because it isn't logically
|part of the type, I usually find defining things this way solves the problems
|of declaring arrays and looping through each item.  Adding new items is simply
|a matter of inserting them before the "Counter" item.

I got a lot of replies to this through e-mail, mostly proposing this solution
(ie, a special symbol last in the enum-list).  It was also pointed out that my
solution of using last_days_t as a name looks like a type name.  I had thought
of this solution before, but I had discarded it because, as you say, the 
"NumThings" value isn't logically a member of ThingList.  However, given the
difficulties presented by my solution, I think that yours is probably best.

Of course, the optimal solution would be for ANSI to adopt a 'numberof'
with functionality similar to 'sizeof', so that you could say 
numberof(ThingList) to get the number of values.

|  -      _   Joe Brownlee, Analysts International Corp. @ AT&T Network Systems
| /_\  @ / `  471 E Broad St, Suite 1610, Columbus, Ohio 43215   (614) 860-7461
|/   \ | \_,  E-mail: jbr at cblph.att.com     Who pays attention to what _I_ say?
|"Scotty, we need warp drive in 3 minutes or we're all dead!" --- James T. Kirk

Dave Schaumann		|  And then -- what then?  Then, future...
dave at cs.arizona.edu	|  		-Weather Report



More information about the Comp.lang.c mailing list