gcc: error: case label does not reduce to an integer constant

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SysSpider

    gcc: error: case label does not reduce to an integer constant

    Hi again,

    This is my problem: when i try to compile the code that contains the
    function below, i get this:

    --
    gcc:21: error: case label does not reduce to an integer constant
    gcc:24: error: case label does not reduce to an integer constant
    --

    Now, I (think that I) understand the error, but cryptMsg[i] surely
    doesn't look like a constant to me...

    Thanks for help,
    SysSpider

    P.S.: btw, for those interested, I'm implementing the "one time pad"
    cryptography system, although that isn't important to the problem...


    --Source code--
    void decryptMsg(int sizeMsg) {
    int i;
    char cryptMsg[sizeMsg], key[sizeMsg];

    printf("\nEnter the encrypted message (no spaces): ");
    scanf("%s", cryptMsg);
    printf("Enter the used key (no spaces): ");
    scanf("%s", key);

    printf("\nThe message is:\n");

    for(i = 0; i < sizeMsg; i++)
    {
    if(i%2 == 0 && i != 0) printf(" ");

    switch(cryptMsg[i]) //Here starts the problem
    {
    case charToInt(crypt Msg[i]) < charToInt(key[i]): //Error
    printf("%d", charToInt(crypt Msg[i]) + 10 -
    charToInt(key[i]));
    break;
    case charToInt(crypt Msg[i]) >= charToInt(key[i]): //Error
    printf("%d", charToInt(crypt Msg[i]) -
    charToInt(key[i]));
    break;
    default:
    printf("\nError !\n");
    break;
    }
    }
    }
    --End of source--
  • Ben Pfaff

    #2
    Re: gcc: error: case label does not reduce to an integer constant

    SysManiacSpider @hackernetwork. com (SysSpider) writes:
    [color=blue]
    > --
    > gcc:21: error: case label does not reduce to an integer constant
    > gcc:24: error: case label does not reduce to an integer constant
    > --[/color]

    Presumably for this line and the similar one below it:
    case charToInt(crypt Msg[i]) < charToInt(key[i]): //Error
    [color=blue]
    > Now, I (think that I) understand the error, but cryptMsg[i] surely
    > doesn't look like a constant to me...[/color]

    Exactly. A case label must be an integer constant. You cannot
    do this sort of test with a switch statement. I suggest using an
    if statement instead.
    --
    "If I've told you once, I've told you LLONG_MAX times not to
    exaggerate."
    --Jack Klein

    Comment

    • Joe Wright

      #3
      Re: gcc: error: case label does not reduce to an integer constant

      SysSpider wrote:[color=blue]
      > Hi again,
      >
      > This is my problem: when i try to compile the code that contains the
      > function below, i get this:
      >
      > --
      > gcc:21: error: case label does not reduce to an integer constant
      > gcc:24: error: case label does not reduce to an integer constant
      > --
      >
      > Now, I (think that I) understand the error, but cryptMsg[i] surely
      > doesn't look like a constant to me...
      >
      > Thanks for help,
      > SysSpider
      >
      > P.S.: btw, for those interested, I'm implementing the "one time pad"
      > cryptography system, although that isn't important to the problem...
      >
      >
      > --Source code--
      > void decryptMsg(int sizeMsg) {
      > int i;
      > char cryptMsg[sizeMsg], key[sizeMsg];
      >
      > printf("\nEnter the encrypted message (no spaces): ");
      > scanf("%s", cryptMsg);
      > printf("Enter the used key (no spaces): ");
      > scanf("%s", key);
      >
      > printf("\nThe message is:\n");
      >
      > for(i = 0; i < sizeMsg; i++)
      > {
      > if(i%2 == 0 && i != 0) printf(" ");
      >
      > switch(cryptMsg[i]) //Here starts the problem
      > {
      > case charToInt(crypt Msg[i]) < charToInt(key[i]): //Error
      > printf("%d", charToInt(crypt Msg[i]) + 10 -
      > charToInt(key[i]));
      > break;
      > case charToInt(crypt Msg[i]) >= charToInt(key[i]): //Error
      > printf("%d", charToInt(crypt Msg[i]) -
      > charToInt(key[i]));
      > break;
      > default:
      > printf("\nError !\n");
      > break;
      > }
      > }
      > }
      > --End of source--[/color]

      It won't work your way Spider, give it up with switch(). C's case
      does not take an expression. It takes a constant. You have to know
      in advance what the cases are looking for.

      switch(getnum() ) {
      case 1: fun(1); break;
      case 2: fun(2); break;
      default: fun(3);
      }

      The canonical 'exclusive case' construct can be done like this..

      if (case1)
      fun(1);
      else if (case2)
      fun(2);
      else
      fun(3);

      ...where case1 and case2 are expressions of 0 or not-0.
      --
      Joe Wright mailto:joewwrig ht@comcast.net
      "Everything should be made as simple as possible, but not simpler."
      --- Albert Einstein ---

      Comment

      • Christopher Benson-Manica

        #4
        Re: gcc: error: case label does not reduce to an integer constant

        Ben Pfaff <blp@cs.stanfor d.edu> spoke thus:
        [color=blue]
        > A case label must be an integer constant.[/color]

        That's the same as saying that case labels must have integer values
        computable at compile time, isn't it? (fmi)

        --
        Christopher Benson-Manica | I *should* know what I'm talking about - if I
        ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

        Comment

        • john_bode@my-deja.com

          #5
          Re: gcc: error: case label does not reduce to an integer constant


          SysSpider wrote:[color=blue]
          > Hi again,
          >
          > This is my problem: when i try to compile the code that contains the
          > function below, i get this:
          >
          > --
          > gcc:21: error: case label does not reduce to an integer constant
          > gcc:24: error: case label does not reduce to an integer constant
          > --
          >
          > Now, I (think that I) understand the error, but cryptMsg[i] surely
          > doesn't look like a constant to me...
          >
          > Thanks for help,
          > SysSpider
          >
          > P.S.: btw, for those interested, I'm implementing the "one time pad"
          > cryptography system, although that isn't important to the problem...
          >
          >
          > --Source code--
          > void decryptMsg(int sizeMsg) {
          > int i;
          > char cryptMsg[sizeMsg], key[sizeMsg];
          >
          > printf("\nEnter the encrypted message (no spaces): ");
          > scanf("%s", cryptMsg);
          > printf("Enter the used key (no spaces): ");
          > scanf("%s", key);
          >
          > printf("\nThe message is:\n");
          >
          > for(i = 0; i < sizeMsg; i++)
          > {
          > if(i%2 == 0 && i != 0) printf(" ");
          >
          > switch(cryptMsg[i]) //Here starts the problem
          > {
          > case charToInt(crypt Msg[i]) < charToInt(key[i]): //Error
          > printf("%d", charToInt(crypt Msg[i]) + 10 -
          > charToInt(key[i]));
          > break;
          > case charToInt(crypt Msg[i]) >= charToInt(key[i]): //Error
          > printf("%d", charToInt(crypt Msg[i]) -
          > charToInt(key[i]));
          > break;
          > default:
          > printf("\nError !\n");
          > break;
          > }
          > }
          > }
          > --End of source--[/color]

          Comment

          • john_bode@my-deja.com

            #6
            Re: gcc: error: case label does not reduce to an integer constant

            Dammit, I *hate* the new Google interface.

            Basically, what you're wanting to use here is an if-else statement,
            *not* a switch:

            if (charToInt(cryp tMsg[i]) < charToInt(keyMs g[i]))
            {
            // do something
            }
            else // charToInt(crypt Msg) must be >= charToInt(keyMs g[i])
            {
            // do something else
            }

            You use a switch when you want to execute code based on the value of
            the thing you're switching on; for example,

            switch (cryptMsg[i])
            {
            case 'A':
            // do something
            break;

            case 'B':
            // do something else
            break;

            ....
            default:
            // default case
            break;
            }

            Case labels must evaluate to an integral constant expression, which a>b
            is *not*.

            Comment

            • Flash Gordon

              #7
              Re: gcc: error: case label does not reduce to an integer constant

              On Tue, 28 Dec 2004 22:05:39 +0000 (UTC)
              Christopher Benson-Manica <ataru@nospam.c yberspace.org> wrote:
              [color=blue]
              > Ben Pfaff <blp@cs.stanfor d.edu> spoke thus:
              >[color=green]
              > > A case label must be an integer constant.[/color]
              >
              > That's the same as saying that case labels must have integer values
              > computable at compile time, isn't it? (fmi)[/color]

              No.

              # cat t.c
              int foo( int x )
              {
              switch (x) {
              case (1,2): return 1;
              case 3: return 2;
              default: return 4;
              }
              }
              # gcc -ansi -pedantic t.c
              t.c: In function `foo':
              t.c:4: error: case label does not reduce to an integer constant
              --
              Flash Gordon
              Living in interesting times.
              Although my email address says spam, it is real and I read it.


              --
              Flash Gordon
              Living in interesting times.
              Although my email address says spam, it is real and I read it.

              Comment

              • Ben Pfaff

                #8
                Re: gcc: error: case label does not reduce to an integer constant

                Christopher Benson-Manica <ataru@nospam.c yberspace.org> writes:
                [color=blue]
                > Ben Pfaff <blp@cs.stanfor d.edu> spoke thus:
                >[color=green]
                >> A case label must be an integer constant.[/color]
                >
                > That's the same as saying that case labels must have integer values
                > computable at compile time, isn't it? (fmi)[/color]

                As far as I can tell, yes. (Are you trying to trap me in some
                kind of Standard lawyerese here, or are you just confused?)

                Here is what the Standard says:
                The expression of each case label shall be an integer
                constant expression...
                --
                int main(void){char p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
                \n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
                );while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
                );}return 0;}

                Comment

                • Ben Pfaff

                  #9
                  Re: gcc: error: case label does not reduce to an integer constant

                  Flash Gordon <spam@flash-gordon.me.uk> writes:
                  [color=blue]
                  > On Tue, 28 Dec 2004 22:05:39 +0000 (UTC)
                  > Christopher Benson-Manica <ataru@nospam.c yberspace.org> wrote:
                  >[color=green]
                  >> Ben Pfaff <blp@cs.stanfor d.edu> spoke thus:
                  >>[color=darkred]
                  >> > A case label must be an integer constant.[/color]
                  >>
                  >> That's the same as saying that case labels must have integer values
                  >> computable at compile time, isn't it? (fmi)[/color]
                  >
                  > No.
                  >
                  > # cat t.c
                  > int foo( int x )
                  > {
                  > switch (x) {
                  > case (1,2): return 1;[/color]

                  Well, the statements are casually and informally equivalent. A
                  case constant like `1+2' is perfectly fine. You found a corner
                  case where an "integer value computable at compile time" is not
                  an "integer constant expression" as defined by the standard,
                  because the comma operator is not allowed in an integer constant
                  expression. But most *useful* constant expressions are valid as
                  case constants.
                  --
                  "The lusers I know are so clueless, that if they were dipped in clue
                  musk and dropped in the middle of pack of horny clues, on clue prom
                  night during clue happy hour, they still couldn't get a clue."
                  --Michael Girdwood, in the monastery

                  Comment

                  • Mark McIntyre

                    #10
                    Re: gcc: error: case label does not reduce to an integer constant

                    On 28 Dec 2004 11:22:22 -0800, in comp.lang.c ,
                    SysManiacSpider @hackernetwork. com (SysSpider) wrote:
                    [color=blue]
                    >Hi again,
                    >
                    > This is my problem: when i try to compile the code that contains the
                    >function below, i get this:
                    >
                    >--
                    >gcc:21: error: case label does not reduce to an integer constant
                    >gcc:24: error: case label does not reduce to an integer constant[/color]

                    Not very surprising. Case labels must be constants. And
                    charToInt(crypt Msg[i]) < charToInt(key[i])
                    isn't a constant.

                    Note that its the LABEL that must be constant, not the case being tested.





                    --
                    Mark McIntyre
                    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
                    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

                    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
                    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
                    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

                    Comment

                    • Mark McIntyre

                      #11
                      Re: gcc: error: case label does not reduce to an integer constant

                      On Tue, 28 Dec 2004 22:05:39 +0000 (UTC), in comp.lang.c , Christopher
                      Benson-Manica <ataru@nospam.c yberspace.org> wrote:
                      [color=blue]
                      >Ben Pfaff <blp@cs.stanfor d.edu> spoke thus:
                      >[color=green]
                      >> A case label must be an integer constant.[/color]
                      >
                      >That's the same as saying that case labels must have integer values
                      >computable at compile time, isn't it? (fmi)[/color]

                      No, its not. They must be actual integer constants. Macros are ok, literals
                      are ok, results of expressions are not.


                      --
                      Mark McIntyre
                      CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
                      CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

                      ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
                      http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
                      ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

                      Comment

                      • Ben Pfaff

                        #12
                        Re: gcc: error: case label does not reduce to an integer constant

                        Mark McIntyre <markmcintyre@s pamcop.net> writes:
                        [color=blue]
                        > On Tue, 28 Dec 2004 22:05:39 +0000 (UTC), in comp.lang.c , Christopher
                        > Benson-Manica <ataru@nospam.c yberspace.org> wrote:
                        >[color=green]
                        >>Ben Pfaff <blp@cs.stanfor d.edu> spoke thus:
                        >>[color=darkred]
                        >>> A case label must be an integer constant.[/color]
                        >>
                        >>That's the same as saying that case labels must have integer values
                        >>computable at compile time, isn't it? (fmi)[/color]
                        >
                        > No, its not. They must be actual integer constants. Macros are ok, literals
                        > are ok, results of expressions are not.[/color]

                        No, you're wrong. Let me repeat the quote from the Standard that
                        I posted earlier:
                        The expression of each case label shall be an integer
                        constant expression...
                        An integer constant expression is not necessarily just a literal.
                        --
                        Go not to Usenet for counsel, for they will say both no and yes.

                        Comment

                        • Christopher Benson-Manica

                          #13
                          Re: gcc: error: case label does not reduce to an integer constant

                          Ben Pfaff <blp@cs.stanfor d.edu> spoke thus:
                          [color=blue]
                          > As far as I can tell, yes. (Are you trying to trap me in some
                          > kind of Standard lawyerese here, or are you just confused?)[/color]

                          That's just how I would have answered the question, and I wanted to
                          know whether I would have been strictly correct (Flash's post suggests
                          that the answer is "no").

                          Me trying to language-lawyer you would be about as dumb as me trying
                          to do it to Dan Pop, or Dennis Ritchie himself for that matter :)

                          --
                          Christopher Benson-Manica | I *should* know what I'm talking about - if I
                          ataru(at)cybers pace.org | don't, I need to know. Flames welcome.

                          Comment

                          • lonny.nettnay@gmail.com

                            #14
                            Re: gcc: error: case label does not reduce to an integer constant

                            Ben Pfaff wrote:
                            [color=blue]
                            > int main(void){char[/color]
                            p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\[color=blue]
                            > \n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int[/color]
                            putchar(\[color=blue]
                            > );while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof[/color]
                            p-1;putchar(p[i]\[color=blue]
                            > );}return 0;}[/color]

                            Hi Ben,

                            I had a little difficulty getting your program to work. You have a
                            'space' character in the variable - char *q - but no space in the
                            variable - char p[]. It took me a little bit of work to figure out that
                            it should go after the 'z.'.

                            Cute program. Thanks for the laugh.

                            Lonny

                            Comment

                            • Mark McIntyre

                              #15
                              Re: gcc: error: case label does not reduce to an integer constant

                              On Tue, 28 Dec 2004 17:25:26 -0800, in comp.lang.c , Ben Pfaff
                              <blp@cs.stanfor d.edu> wrote:
                              [color=blue]
                              >Mark McIntyre <markmcintyre@s pamcop.net> writes:
                              >[color=green]
                              >> On Tue, 28 Dec 2004 22:05:39 +0000 (UTC), in comp.lang.c , Christopher
                              >> Benson-Manica <ataru@nospam.c yberspace.org> wrote:
                              >>[color=darkred]
                              >>>Ben Pfaff <blp@cs.stanfor d.edu> spoke thus:
                              >>>
                              >>>> A case label must be an integer constant.
                              >>>
                              >>>That's the same as saying that case labels must have integer values
                              >>>computable at compile time, isn't it? (fmi)[/color]
                              >>
                              >> No, its not. They must be actual integer constants. Macros are ok, literals
                              >> are ok, results of expressions are not.[/color]
                              >
                              >No, you're wrong.[/color]

                              Possibly. But I disagree.
                              [color=blue]
                              >Let me repeat the quote from the Standard that
                              >I posted earlier:[/color]

                              I saw it earlier. :-)
                              [color=blue]
                              >An integer constant expression is not necessarily just a literal.[/color]

                              I didn't say it had to be. I'd be interested in examples of suitable
                              expressions you could use.

                              --
                              Mark McIntyre
                              CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
                              CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

                              ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
                              http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
                              ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

                              Comment

                              Working...