Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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
 
Reply With Quote
 
 
 
 
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.
 
Reply With Quote
 
 
 
 
SM Ryan
Guest
Posts: n/a
 
      10-13-2005
Francois Grieu <(E-Mail Removed)> 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]);

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I love the smell of commerce in the morning.
 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      10-13-2005
SM Ryan wrote:
>
> Francois Grieu <(E-Mail Removed)> 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: <(E-Mail Removed)>

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-13-2005
Francois Grieu <(E-Mail Removed)> 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) http://www.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      10-14-2005
On Thu, 13 Oct 2005 11:18:42 -0400, Kenneth Brody
<(E-Mail Removed)> wrote in comp.lang.c:

> SM Ryan wrote:
> >
> > Francois Grieu <(E-Mail Removed)> 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.


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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can Groovy be used in an applet and/or can it generate the Java bytecodes that then can be used in an applet? Casey Hawthorne Java 1 03-18-2009 12:56 AM
Has anyone used enum.h from Besser's ENUM++? Randy C++ 2 01-08-2006 03:51 AM
enum: display elements of an enum specified at runtime Jerminia Java 3 10-07-2005 10:08 PM
enum within an enum - Java 6 06-13-2005 12:51 AM
How to enum an enum? Ernst Murnleitner C++ 5 11-13-2003 11:06 AM



Advertisments