Can an enum be used as an array size?

Discussion in 'C Programming' started by Francois Grieu, Oct 13, 2005.

  1. 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
     
    Francois Grieu, Oct 13, 2005
    #1
    1. Advertising

  2. 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.
     
    Emmanuel Delahaye, Oct 13, 2005
    #2
    1. Advertising

  3. Francois Grieu

    SM Ryan Guest

    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);

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    I love the smell of commerce in the morning.
     
    SM Ryan, Oct 13, 2005
    #3
  4. 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);


    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: <mailto:>
     
    Kenneth Brody, Oct 13, 2005
    #4
  5. 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) <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.
     
    Keith Thompson, Oct 13, 2005
    #5
  6. Francois Grieu

    Jack Klein Guest

    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
    > >
    > > 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);

    >
    > 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/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Oct 14, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -

    enum within an enum

    -, Jun 12, 2005, in forum: Java
    Replies:
    6
    Views:
    591
  2. Jerminia
    Replies:
    3
    Views:
    659
    Roedy Green
    Oct 7, 2005
  3. Ernst Murnleitner

    How to enum an enum?

    Ernst Murnleitner, Nov 12, 2003, in forum: C++
    Replies:
    5
    Views:
    507
    Rolf Magnus
    Nov 13, 2003
  4. Randy
    Replies:
    2
    Views:
    389
    Randy Yates
    Jan 8, 2006
  5. Casey Hawthorne
    Replies:
    1
    Views:
    754
    Arne Vajhøj
    Mar 18, 2009
Loading...

Share This Page