473,503 Members | 11,435 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is that a constant expression?

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.
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Jul 22 '05 #1
12 1720

"Alexander Malkis" <al******@LOESCHEDIESline.cs.uni-sb.de> wrote in message
news:cl****************@plethora.net...
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

Jul 22 '05 #2
No. Not yet.
Thank you.
Alex.

Mike Wahler wrote:

"Alexander Malkis" <al******@LOESCHEDIESline.cs.uni-sb.de> wrote in message
news:cl****************@plethora.net...
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

Jul 22 '05 #3

"Alexander Malkis" <al******@LOESCHEDIESline.cs.uni-sb.de> skrev i
meddelandet news:cl****************@plethora.net...
Assume we have somewhere
typedef enum foo { A,B,C } foo;

Is C+1 a constant expression?


That depends.
If the built-in operator+ is used, it is. If you have written an own
operator+(foo, int), it isn't.

--
Dag Henriksson
Jul 22 '05 #4
On 26 Feb 2004 21:56:14 GMT, Alexander Malkis
<al******@LOESCHEDIESline.cs.uni-sb.de> wrote:
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
le**@bdsoft.com
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
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Jul 22 '05 #5
Alexander Malkis wrote:
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
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Jul 22 '05 #6
Alexander Malkis wrote:
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
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Jul 22 '05 #7
[F'up2 cut down --- should have been done by OP!]

In comp.lang.c.moderated Alexander Malkis wrote:
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"};
--
Hans-Bernhard Broeker (br*****@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Jul 22 '05 #8
In <cl****************@plethora.net> Alexander Malkis <al******@LOESCHEDIESline.cs.uni-sb.de> writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Jul 22 '05 #9
[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.

--
a signature
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Jul 22 '05 #10
Alexander Malkis schrieb:
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
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Jul 22 '05 #11
Jeff Schwab wrote:
Alexander Malkis wrote:
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
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Jul 22 '05 #12
Tor Husabø wrote:
Jeff Schwab wrote:
Alexander Malkis wrote:
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.
--
comp.lang.c.moderated - moderation address: cl**@plethora.net
Jul 22 '05 #13

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

Similar topics

15
3241
by: Chris Saunders | last post by:
I have made a declaration like this: private const Complex I = new Complex(0.0, 1.0); When I try to build this I get the error: The expression being assigned to...
13
2543
by: devdatta_clc | last post by:
Hi C experts I've a bunch of questions. Consider this simplified piece of code. const int a = 10; int main () { static int b = a;
1
3051
by: Andrzej 'Foxy' D. | last post by:
Hi! I cannot figure out, why is the following code incorrect: const int array = { 1 }; const int var = 1; template<int I> struct Blah {};
3
2335
by: lovecreatesbeauty | last post by:
Both `K&R C, 2nd' and `C: A reference manual, 5th' introduce the "hello, world" thing using the name "string-constant". But `ISO/IEC 9899:TC2' does not include this kind of thing in section `A.1.5...
3
4748
by: Dan Smithers | last post by:
What constitutes a constant-expression? I know that it is something that can be determined at compile time. I am trying to use template code and keep getting compiler errors "error: cannot...
7
2792
by: John Koleszar | last post by:
Hi all, I'm porting some code that provides compile-time assertions from one compiler to another and ran across what I believe to be compliant code that won't compile using the new compiler. Not...
7
4248
by: Hendrik Schober | last post by:
Hi, this #include <string> class test { typedef std::string::size_type size_type; static const size_type x = std::string::npos; }; doesn't compile using either VC9 ("expected constant...
56
6659
by: Adem | last post by:
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15) says under...
8
20392
by: Stefano Sabatini | last post by:
Hi all, I'm encountering this while trying to implement a factory singleton method to generate objects. The singleton has a static map which binds a static creation function defined in each...
8
13388
by: PJ6 | last post by:
Const factor As Double = Math.Sqrt(3) / 6 Error 1 Constant expression is required. This looks like laziness to me. In SQL Server, functions are given a distinction between ones that always...
0
7193
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
7264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6975
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
5562
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,...
1
4992
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...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.