SM Ryan wrote:
>
> Francois Grieu <> wrote:
> # Can an enum be used as an array size?
> #
> # In other word, is this legal?
> #
> # enum {n=1};
> # int a[n];
> # int main(void){return a[0];}
>
> Yes. You can also do something like
>
> enum {brown,red,orange,yellowwhite,white,bluewhite,num_ radiances} radiance;
> char* star[num_radiances];
> star[brown] = "dwarf";
> star[red] = "betelguese";
> star[yellowwhite] = "sol";
> star[bluewhite] = "sirius";
> ...
> enum radiance i;
> for (i=brown; i<=bluewhite; i++) puts(star[i]);
s/i<=bluewhite/i<num_radiances/
(This will allow to code to continue working when new radiances are added,
for the same reason you didn't use "char* star[bluewhite+1]".)
And you should probably not hard-code "brown" as the starting value, either.
enum {first_radiance,
brown=first_radiance,red,orange,yellowwhite,white, bluewhite,
num_radiances} radiance;
...
for ( i = first_radiance, i < num_radiance ; i++ )
...
The construct "enum { foo, bar=foo } foobar" works on my compiler. Is this
guaranteed by the standard? I've never had need to use such a construct
before now.
--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody |
www.hvcomputer.com | |
| kenbrody/at\spamcop.net |
www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <private.php?do=newpm&u=>