how to do with this case?

Q

qianz99

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!
 
M

Mike Wahler

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
 
F

Flash Gordon

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:
 
K

Keith Thompson

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.
 

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
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top