Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!jarthur!elroy.jpl.nasa.gov!ncar!noao!arizona!dave From: dave@cs.arizona.edu (Dave P. Schaumann) Newsgroups: comp.lang.c Subject: Re: Number of enums Message-ID: <708@caslon.cs.arizona.edu> Date: 21 Jan 91 23:51:00 GMT References: <703@caslon.cs.arizona.edu> <1991Jan21.133917.18458@cbnews.att.com> Organization: U of Arizona CS Dept, Tucson Lines: 40 In article <1991Jan21.133917.18458@cbnews.att.com> jbr@cblph.att.com writes: |In article <703@caslon.cs.arizona.edu| dave@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@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@cs.arizona.edu | -Weather Report