qi*****@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)
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.