Can an enum be used as an array size?

F

Francois Grieu

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
 
E

Emmanuel Delahaye

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

SM Ryan

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

Kenneth Brody

SM said:
# 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:[email protected]>
 
K

Keith Thompson

Francois Grieu said:
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 };
 
J

Jack Klein

SM said:
# 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top