Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   how to do with this case? (http://www.velocityreviews.com/forums/t439782-how-to-do-with-this-case.html)

qianz99@gmail.com 10-13-2005 05:06 PM

how to do with this case?
 
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!


Mike Wahler 10-13-2005 06:25 PM

Re: how to do with this case?
 

<qianz99@gmail.com> wrote in message
news:1129223216.469333.154200@g14g2000cwa.googlegr 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



Flash Gordon 10-13-2005 07:03 PM

Re: how to do with this case?
 
qianz99@gmail.com 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.

Keith Thompson 10-13-2005 08:29 PM

Re: how to do with this case?
 
qianz99@gmail.com 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) kst-u@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.


All times are GMT. The time now is 10:55 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.