Getting the number of elements in an enumerated type.

Richard A. O'Keefe ok at goanna.cs.rmit.oz.au
Mon Oct 29 13:22:28 AEST 1990


In article <6837 at castle.ed.ac.uk> elee24 at castle.ed.ac.uk (H Bruce) writes:
> Can you automatically get the number of elements in an enumerated type ?
In article <1990Oct26.154448.26698 at zoo.toronto.edu>, henry at 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.



More information about the Comp.lang.c mailing list