how to do with this case?

Discussion in 'C Programming' started by qianz99@gmail.com, Oct 13, 2005.

  1. Guest

    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!
     
    , Oct 13, 2005
    #1
    1. Advertising

  2. Mike Wahler Guest

    <> wrote in message
    news:...
    > 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
     
    Mike Wahler, Oct 13, 2005
    #2
    1. Advertising

  3. Flash Gordon Guest

    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.
     
    Flash Gordon, Oct 13, 2005
    #3
  4. 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) <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.
     
    Keith Thompson, Oct 13, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Franks
    Replies:
    2
    Views:
    1,285
    Steve Franks
    Jun 10, 2004
  2. Tee
    Replies:
    3
    Views:
    7,913
    Herfried K. Wagner [MVP]
    Jun 23, 2004
  3. Janice

    lower case to upper case

    Janice, Dec 10, 2004, in forum: C Programming
    Replies:
    17
    Views:
    1,227
    Richard Bos
    Dec 14, 2004
  4. Replies:
    1
    Views:
    2,556
    Mark P
    Apr 6, 2007
  5. darrel

    'case' vs. 'case is'

    darrel, Aug 14, 2008, in forum: ASP .Net
    Replies:
    3
    Views:
    295
    Rory Becker
    Aug 14, 2008
Loading...

Share This Page