Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > how to do with this case?

Reply
Thread Tools

how to do with this case?

 
 
qianz99@gmail.com
Guest
Posts: n/a
 
      10-13-2005
Hello I define a sequence of const
typedef struct
{
int year;
int month;
}birth
const birth a={1990,1};
const birth b={1990,2};
......


Now I'd like to use switch in my main function


I have

switch(test)
{
case a.month: ...
case b.month: ...
...
}

but the compiler says :
case label does not reduce to an integer constant

What should I do?
I don't want to use if.

Thanks a lot!

 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      10-13-2005

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Hello I define a sequence of const
> typedef struct
> {
> int year;
> int month;
> }birth


} birth;

> const birth a={1990,1};
> const birth b={1990,2};
> .....
>
>
> Now I'd like to use switch in my main function
>
>
> I have
>
> switch(test)
> {
> case a.month: ...
> case b.month: ...
> ...
> }
>
> but the compiler says :
> case label does not reduce to an integer constant


Right. They must be integer constants.

>
> What should I do?


Use integer constants, or some other mechanism
which doesn't require them, e.g. 'if'.

> I don't want to use if.


Why not? Are you under the misconception that 'switch'
is somehow inherently 'better' than 'if'?

Perhaps if you give more context about exactly what
you want your program to do, we could offer specific
advice on structuring your code.

-Mike


 
Reply With Quote
 
 
 
 
Flash Gordon
Guest
Posts: n/a
 
      10-13-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hello I define a sequence of const
> typedef struct
> {
> int year;
> int month;
> }birth
> const birth a={1990,1};
> const birth b={1990,2};
> .....


const in C specifies that an object is not modifiable, it does not
create a true compile time constant.

> Now I'd like to use switch in my main function
>
>
> I have
>
> switch(test)
> {
> case a.month: ...


Your cases must be compile time constants.

> case b.month: ...
> ...
> }
>
> but the compiler says :
> case label does not reduce to an integer constant
>
> What should I do?
> I don't want to use if.


So use a constant, not a const qualified object.

#define A_MONTH (1)
const birth a={1990,A_MONTH};
....
switch (test)
case A_MONTH:
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-13-2005
(E-Mail Removed) writes:
> Hello I define a sequence of const
> typedef struct
> {
> int year;
> int month;
> }birth
> const birth a={1990,1};
> const birth b={1990,2};
> .....
>
>
> Now I'd like to use switch in my main function
>
>
> I have
>
> switch(test)
> {
> case a.month: ...
> case b.month: ...
> ...
> }
>
> but the compiler says :
> case label does not reduce to an integer constant
>
> What should I do?
> I don't want to use if.


Declaring something as "const" doesn't make it a constant. It's
something best thought of as a read-only variable. Yes, it would be
nice if you could do what you're trying to do; unfortunately, the
language doesn't allow it.

You probably need to use a chain of if/else statements -- which isn't
such a bad thing, really.

If you really want to use case, you'll have to use constant
expressions. For example:

#define A_YEAR 1990
#define A_MONTH 1
#define B_YEAR 1991
#define B_MONTH 2
const birth a = {A_YEAR, A_MONTH};
const birth b = {B_YEAR, B_MONTH};

There's another trick that avoids the use of macros:

enum { A_YEAR = 1990, A_MONTH = 1,
B_YEAR = 1991, B_MONTH = 2 };
const birth a = { A_YEAR, A_MONTH };
const birth b = { B_YEAR, B_MONTH };

This only works for values of type int.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM



Advertisments