[comp.lang.c] Getting the number of elements in an enumerated type.

elee24@castle.ed.ac.uk (H Bruce) (10/25/90)

Can you automatically get the number of elements in an enumerated type ?

Thanks,


Henry Bruce.

henry@zoo.toronto.edu (Henry Spencer) (10/26/90)

In article <6837@castle.ed.ac.uk> elee24@castle.ed.ac.uk (H Bruce) writes:
>Can you automatically get the number of elements in an enumerated type ?

No.
-- 
The type syntax for C is essentially   | Henry Spencer at U of Toronto Zoology
unparsable.             --Rob Pike     |  henry@zoo.toronto.edu   utzoo!henry

scc@rlgvax.UUCP (Stephen Carlson) (10/27/90)

In article <6837@castle.ed.ac.uk> elee24@castle.ed.ac.uk (H Bruce) writes:
>Can you automatically get the number of elements in an enumerated type ?

No, not automatically.

But if you define your enums without explicit values, just add an extra
element to the end like this:

			 /* 0    1      2   */
	enum foo { bar, baz, max_foo };

Here, max_foo will have the integer value of the number of elements (not
counting max_foo) in the enum.  There are three elements in this enum,
the two real ones and the third which holds the number of elements.  The
elegance of C's zero-based system really shines through in this example.

On a non-fascist compiler (one that does not complain about pointer arithmetic
with enums), you can do:

	int a[max_foo];
	enum foo ix;

	for (ix = bar; ix < max_foo; ix++)
		a[ix] = 0;

	if (some_condition())
		a[bar] = 2;

This approach works well with using enums to index arrays.

I hope this helps.
-- 
Stephen Carlson            | ICL OFFICEPOWER Center
scc@rlgvax.opcr.icl.com    | 11490 Commerce Park Drive
..!uunet!rlgvax!scc        | Reston, VA  22091

akcs.bagger@vpnet.chi.il.us (Brother Bagger) (10/27/90)

	Actually its fairly simple.
	typedef enum {val1=0,val2,val3,val4,val5,VALUE_COUNT} values;

VALUE_COUNT will equal 5 and as long as you don't change the sequental ordering or move VALUE_COUNT from the end of the list.

	Brian

ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) (10/29/90)

In article <6837@castle.ed.ac.uk> elee24@castle.ed.ac.uk (H Bruce) writes:
> Can you automatically get the number of elements in an enumerated type ?
In article <1990Oct26.154448.26698@zoo.toronto.edu>, henry@zoo.toronto.edu (Henry Spencer) writes:
: No.

Just to add to this:  it isn't clear what the question _means_.
Consider an Ada example:
	type EPNEUM is (FOO,BAZ,UGH);
	type UREY is array(EPNEUM) of CHARACTER
With these definitions,
	EPNEUM'POS(EPNEUM'LAST) + 1

	EPNEUM'POS(EPNEUM'LAST) - EPNEUM'POS(EPNEUM'FIRST) + 1
and
	UREY'SIZE / CHARACTER'SIZE
coincide (actually, I'm not 100% sure about the last), and all correspond
to the intuitive notion "number of elements in an enumerated type".

But now consider a C example:
	enum foo { a = -900, b = -87, c = -3 };
c+1 is -2, c-a+1 is -896, and neither of them is 3.  Worse still,
how about
	enum baz {a = 0, b = 0, c = 0, d = 0 };
Has this four "elements", or one?

-- 
Fear most of all to be in error.	-- Kierkegaard, quoting Socrates.