Path: utzoo!attcan!uunet!munnari.oz.au!goanna!ok From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) Newsgroups: comp.lang.c Subject: Re: Getting the number of elements in an enumerated type. Message-ID: <4111@goanna.cs.rmit.oz.au> Date: 29 Oct 90 02:22:28 GMT References: <6837@castle.ed.ac.uk> <1990Oct26.154448.26698@zoo.toronto.edu> Organization: Comp Sci, RMIT, Melbourne, Australia Lines: 27 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.