Is that a constant expression?

A

Alexander Malkis

Assume we have somewhere
typedef enum foo { A,B,C } foo;

Is C+1 a constant expression?

[
I want to write smth. like
char* strs[C+1]={"Astr","Bstr","Cstr"};
]

Could anyone help?
Regards,
Alex.
 
M

Mike Wahler

Alexander Malkis said:
Assume we have somewhere
typedef enum foo { A,B,C } foo;

Is C+1 a constant expression?
Yes.


[
I want to write smth. like
char* strs[C+1]={"Astr","Bstr","Cstr"};
]

Could anyone help?

It looks OK to me. Are you having trouble with it?

-Mike
 
L

Leor Zolman

Assume we have somewhere
typedef enum foo { A,B,C } foo;

Is C+1 a constant expression?
Yes.


[
I want to write smth. like
char* strs[C+1]={"Astr","Bstr","Cstr"};
]

Could anyone help?

Well, you start up your text editor, and then type it in ;-)

Seriously, though, wouldn't just trying this in your compiler have been
less work than entering the names of all those newsgroups you've
cross-posted to?

Regards,
Alex.

Leor Zolman
BD Software
(e-mail address removed)
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
 
T

Thomas Matthews

Alexander said:
Assume we have somewhere
typedef enum foo { A,B,C } foo;

Is C+1 a constant expression?

[
I want to write smth. like
char* strs[C+1]={"Astr","Bstr","Cstr"};
]

Could anyone help?
Regards,
Alex.

What's wrong with letting the compiler figure
out the number of elements?

char * strs[] = {"Astr", "Bstr", "Cstr"};

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
J

Jeff Schwab

Alexander said:
typedef enum foo { A,B,C } foo;
.....

char* strs[C+1]={"Astr","Bstr","Cstr"};


NB: The following refers to C++. I don't know the answer in C.

Your approach almost certainly will work in this particular case.
However, it may not work in the general case. Enums are not guaranteed
to have any minimum number of bits, as long as they are wide enough to
hold all of their enumerators.

Here is a common alternative:

enum Foo { a, b, c, num_foo };

-Jeff
 
H

Hans-Bernhard Broeker

[F'up2 cut down --- should have been done by OP!]
Assume we have somewhere
typedef enum foo { A,B,C } foo;
Is C+1 a constant expression?

Yes. What makes you suspect otherwise?
I want to write smth. like
char* strs[C+1]={"Astr","Bstr","Cstr"};

You don't need C+1 to be a constant, for that. Actually, you don't
need C+1 at all:

char *strs[] = {"Astr", "Bstr", "Cstr"};
 
D

Dan Pop

In said:
Assume we have somewhere
typedef enum foo { A,B,C } foo;

Is C+1 a constant expression?
Yes.

[
I want to write smth. like
char* strs[C+1]={"Astr","Bstr","Cstr"};
]

What's wrong with:

char *strs[] = {"Astr", "Bstr", "Cstr"};

?

Dan
 
T

those who know me have no need of my name

[ugh -- horribly cross-posted and no followup-to]

in comp.lang.c.moderated i read:
Assume we have somewhere
typedef enum foo { A,B,C } foo;

Is C+1 a constant expression?

yes.
 
C

Claus Reibenstein

Alexander said:
Is C+1 a constant expression?

Where do you want to find the answer? I see no fup2. And obviously
no-one so far recognized your crossposting anyway. Poor blind guys.

Fup2 ... hmmm ... where? ... let's see ... yes, that seems adequate.

Claus
 
?

=?ISO-8859-1?Q?Tor_Husab=F8?=

Jeff said:
Alexander said:
typedef enum foo { A,B,C } foo;

....

char* strs[C+1]={"Astr","Bstr","Cstr"};



NB: The following refers to C++. I don't know the answer in C.

Your approach almost certainly will work in this particular case.
However, it may not work in the general case. Enums are not guaranteed
to have any minimum number of bits, as long as they are wide enough to
hold all of their enumerators.

Just for the record, an enum in C, unlike in C++, is always an int. And
the size enum foo doesn't matter here anyway.

Tor
 
J

Jeff Schwab

Tor said:
Jeff said:
Alexander said:
typedef enum foo { A,B,C } foo;


....

char* strs[C+1]={"Astr","Bstr","Cstr"};


NB: The following refers to C++. I don't know the answer in C.

Your approach almost certainly will work in this particular case.
However, it may not work in the general case. Enums are not
guaranteed to have any minimum number of bits, as long as they are
wide enough to hold all of their enumerators.


Just for the record, an enum in C, unlike in C++, is always an int. And
the size enum foo doesn't matter here anyway.

Yes, you're right. Of course, if A, B, and C were macros representing
expressions with large initializers, or if int's were only one bit
wide... :)

It is true, though, that the size of the array doesn't need to be
specified explicitly here anyway.
 

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
474,164
Messages
2,570,901
Members
47,439
Latest member
elif2sghost

Latest Threads

Top