Velocity Reviews > Can an enum be used as an array size?

# Can an enum be used as an array size?

Francois Grieu
Guest
Posts: n/a

 10-13-2005
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];}

TIA

François Grieu

Emmanuel Delahaye
Guest
Posts: n/a

 10-13-2005
Francois Grieu a écrit :
> 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, because an enum is a constant expression. But be aware that an enum
can't be bigger that an int and that an int can be smaller than a size_t.

SM Ryan
Guest
Posts: n/a

 10-13-2005
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

star[brown] = "dwarf";
star[red] = "betelguese";
star[yellowwhite] = "sol";
star[bluewhite] = "sirius";
...
for (i=brown; i<=bluewhite; i++) puts(star[i]);

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I love the smell of commerce in the morning.

Kenneth Brody
Guest
Posts: n/a

 10-13-2005
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
>
> star[brown] = "dwarf";
> star[red] = "betelguese";
> star[yellowwhite] = "sol";
> star[bluewhite] = "sirius";
> ...
> for (i=brown; i<=bluewhite; i++) puts(star[i]);

(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.

...

...

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=>

Keith Thompson
Guest
Posts: n/a

 10-13-2005
Francois Grieu <> writes:
> 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. An enumeration constant like "n" is actually a constant of type
int, not, as you might expect, a constant of the enumeration type. So
int a[n];
is equivalent to
int a[1];

It's possible to take advantage of this to declare integer constants
without using macros:

enum { MAX = 1000 };

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Jack Klein
Guest
Posts: n/a

 10-14-2005
On Thu, 13 Oct 2005 11:18:42 -0400, Kenneth Brody
<> wrote in comp.lang.c:

> 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
> >
> > star[brown] = "dwarf";
> > star[red] = "betelguese";
> > star[yellowwhite] = "sol";
> > star[bluewhite] = "sirius";
> > ...
> > for (i=brown; i<=bluewhite; i++) puts(star[i]);

>
>
>
>
> (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.
>
>
> ...
>
> 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.

Yes. The identifier becomes an integer constant expression at the end
of its individual definition, which means either the '}' ending the
enum definition, or the ',' following its definition or enumeration.

So:

enum rgb = { red = 1, green = 2, blue = 4 };

....is equivalent to:

enum rgb = { red = 1, green = red + red; blue = green + green };

Just one of C's wacky quirks.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html