473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Can an enum be used as an array size?

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
Nov 15 '05 #1
5 9575
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.
Nov 15 '05 #2
Francois Grieu <fg****@francenet.fr> 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.
Nov 15 '05 #3
SM Ryan wrote:

Francois Grieu <fg****@francenet.fr> 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: <mailto:Th*************@gmail.com>

Nov 15 '05 #4
Francois Grieu <fg****@francenet.fr> 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) ks***@mib.org <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.
Nov 15 '05 #5
On Thu, 13 Oct 2005 11:18:42 -0400, Kenneth Brody
<ke******@spamcop.net> wrote in comp.lang.c:
SM Ryan wrote:

Francois Grieu <fg****@francenet.fr> 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
Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Alexander Grigoriev | last post by:
Not quite new version of GCC that I have to use, craps with the following code: enum E; enum E { e }; That is, it doesn't accept forward declaration of enum. C++ standard text doesn't...
7
by: Aryeh M. Friedman | last post by:
If have something like the following declartion: enum foo {One,Two,....,Ten}; How do I determine how many elements (enumerations)... in this case it is obviously 10 but I don't want to hard...
10
by: dof | last post by:
I'm trying the following and having problems. I get errors on the array declaration lines. Something about an array must have at least one element. Thanks in advance. D #include stuff .... ...
18
by: Nebula | last post by:
Consider enum Side {Back,Front,Top,Bottom}; enum Side a; Now, why is a = 124; legal (well it really is an integer, but still, checking could be performed..) ? Wouldn't enums be more useful if...
3
by: Richard | last post by:
Okay gang, This should be simple but apparently it's not... I want to use the System.DayOfWeek enum to create and access an array of objects with one object for each day of the week. I'd like...
1
by: PSN | last post by:
can any one tell me if there is a way to redeclare a typedef enum to add new elements at the end .. Ex: "test.h" typedef enum test1 { hcone = 0, hctwo hcthree, hcfour,
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
5
by: Francois Grieu | last post by:
Does this reliably cause a compile-time error when int is not 4 bytes ? enum { int_size_checked = 1/(sizeof(int)==4) }; Any better way to check the value of an expression involving sizeof...
6
by: Zytan | last post by:
I have an enum. I want a struct of data associated with each one. Then, I could use the enum to access the data as needed. In other words, I want the enum to represent more than just a unique...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.