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

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

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



    Mark McIntyre wrote:[color=blue]
    > On Tue, 28 Dec 2004 17:25:26 -0800, in comp.lang.c , Ben Pfaff
    > <blp@cs.stanfor d.edu> wrote:
    >[color=green]
    >>Mark McIntyre <markmcintyre@s pamcop.net> writes:
    >>[color=darkred]
    >>>On Tue, 28 Dec 2004 22:05:39 +0000 (UTC), in comp.lang.c , Christopher
    >>>Benson-Manica <ataru@nospam.c yberspace.org> wrote:
    >>>
    >>>>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
    >>>>computabl e at compile time, isn't it? (fmi)
    >>>
    >>>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]

    And you are wrong.

    [color=blue][color=green]
    >>Let me repeat the quote from the Standard that
    >>I posted earlier:[/color]
    >
    > I saw it earlier. :-)[/color]

    Then parse it again and try to get the semantics right this time ;-)

    [color=blue][color=green]
    >>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.[/color]

    Everything that counts as a constant expression from
    1
    over
    3<<2
    to
    (7+5)/6*4
    Instead of the numbers, you of course can also use
    symbolic and enumeration constants.


    Cheers
    Michael
    --
    E-Mail: Mine is a gmx dot de address.

    Comment

    • Lawrence Kirby

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

      On Wed, 29 Dec 2004 16:23:08 +0100, Michael Mair wrote:

      ....
      [color=blue]
      > Everything that counts as a constant expression from
      > 1
      > over
      > 3<<2[/color]

      Things like

      1U << 3

      can be useful for bitmask constants.
      [color=blue]
      > to
      > (7+5)/6*4
      > Instead of the numbers, you of course can also use
      > symbolic and enumeration constants.[/color]

      A couple more examples

      sizeof array / sizeof *array /* Where array isn't a VLA */

      (int)1.2345

      Lawrence

      Comment

      • Ben Pfaff

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

        lonny.nettnay@g mail.com writes:
        [color=blue]
        > 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.'.[/color]

        I'm not sure what you're talking about. The program, as posted,
        is this:

        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;}

        There is a space at the beginning of the second line, which puts
        it between the '.' and the '\n' in p[]. Perhaps you or your
        newsreader dropped that somehow before compiling it.
        --
        "Large amounts of money tend to quench any scruples I might be having."
        -- Stephan Wilms

        Comment

        • Keith Thompson

          #19
          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 17:25:26 -0800, in comp.lang.c , Ben Pfaff
          > <blp@cs.stanfor d.edu> wrote:[color=green]
          >>Mark McIntyre <markmcintyre@s pamcop.net> writes:[color=darkred]
          >>> On Tue, 28 Dec 2004 22:05:39 +0000 (UTC), in comp.lang.c , Christopher
          >>> Benson-Manica <ataru@nospam.c yberspace.org> wrote:
          >>>>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
          >>>>computabl e at compile time, isn't it? (fmi)
          >>>
          >>> 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]
          [...][color=blue][color=green]
          >>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.[/color]

          2+2

          --
          Keith Thompson (The_Other_Keit h) 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.

          Comment

          • Mark McIntyre

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

            On Wed, 29 Dec 2004 16:23:08 +0100, in comp.lang.c , Michael Mair
            <Michael.Mair@i nvalid.invalid> wrote:
            [color=blue]
            >
            >
            >Mark McIntyre wrote:[color=green]
            >>
            >> I saw it earlier. :-)[/color]
            >
            >Then parse it again and try to get the semantics right this time ;-)[/color]

            And parse my answer again, and try to understand it right this time :-)
            [color=blue][color=green]
            >>I'd be interested in examples of suitable
            >> expressions you could use.[/color]
            >
            >Everything that counts as a constant expression from
            > 1
            >over
            > 3<<2
            >to
            > (7+5)/6*4[/color]

            Er, these are all literals (as far as I'm concerned). I suspect we have a
            nomenclature disagreement.


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

            Comment

            • Mark McIntyre

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

              On Wed, 29 Dec 2004 21:50:50 GMT, in comp.lang.c , Keith Thompson
              <kst-u@mib.org> wrote:
              [color=blue]
              >Mark McIntyre <markmcintyre@s pamcop.net> writes:[color=green]
              >> I didn't say it had to be. I'd be interested in examples of suitable
              >> expressions you could use.[/color]
              >
              >2+2[/color]

              Thats a literal in my book.


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

              Comment

              • Ben Pfaff

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

                Mark McIntyre <markmcintyre@s pamcop.net> writes:
                [color=blue]
                > On Wed, 29 Dec 2004 16:23:08 +0100, in comp.lang.c , Michael Mair
                > <Michael.Mair@i nvalid.invalid> wrote:[color=green]
                >>Everything that counts as a constant expression from
                >> 1
                >>over
                >> 3<<2
                >>to
                >> (7+5)/6*4[/color]
                >
                > Er, these are all literals (as far as I'm concerned). I suspect we have a
                > nomenclature disagreement.[/color]

                You'll have to define "literal" for the rest of us then. The
                standard only uses it in the phrases "string literal" and
                "compound literal".
                --
                "It wouldn't be a new C standard if it didn't give a
                new meaning to the word `static'."
                --Peter Seebach on C99

                Comment

                • Keith Thompson

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

                  Mark McIntyre <markmcintyre@s pamcop.net> writes:[color=blue]
                  > On Wed, 29 Dec 2004 21:50:50 GMT, in comp.lang.c , Keith Thompson
                  > <kst-u@mib.org> wrote:
                  >[color=green]
                  >>Mark McIntyre <markmcintyre@s pamcop.net> writes:[color=darkred]
                  >>> I didn't say it had to be. I'd be interested in examples of suitable
                  >>> expressions you could use.[/color]
                  >>
                  >>2+2[/color]
                  >
                  > Thats a literal in my book.[/color]

                  Get a new book.

                  As Ben Pfaff points out, the standard only uses the term in the
                  phrases "string literal" and "compound literal". Based on the usage
                  of the term in other languages, I tend to think of a literal as a
                  single token representing a constant value, what C usually calls a
                  "constant" (integer constant, character constant, etc.) (though that
                  excludes C's compound literals).

                  The term is admittedly ambiguous, but I suspect you're the only person
                  around here who's going to think that 2+2 is a "literal". The
                  standard has a perfectly good term for the concept we're discussing,
                  "constant expression".

                  --
                  Keith Thompson (The_Other_Keit h) 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.

                  Comment

                  • Joe Wright

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

                    Keith Thompson wrote:[color=blue]
                    > Mark McIntyre <markmcintyre@s pamcop.net> writes:
                    >[color=green]
                    >>On Wed, 29 Dec 2004 21:50:50 GMT, in comp.lang.c , Keith Thompson
                    >><kst-u@mib.org> wrote:
                    >>
                    >>[color=darkred]
                    >>>Mark McIntyre <markmcintyre@s pamcop.net> writes:
                    >>>
                    >>>>I didn't say it had to be. I'd be interested in examples of suitable
                    >>>>expressio ns you could use.
                    >>>
                    >>>2+2[/color]
                    >>
                    >>Thats a literal in my book.[/color]
                    >
                    >
                    > Get a new book.
                    >
                    > As Ben Pfaff points out, the standard only uses the term in the
                    > phrases "string literal" and "compound literal". Based on the usage
                    > of the term in other languages, I tend to think of a literal as a
                    > single token representing a constant value, what C usually calls a
                    > "constant" (integer constant, character constant, etc.) (though that
                    > excludes C's compound literals).
                    >
                    > The term is admittedly ambiguous, but I suspect you're the only person
                    > around here who's going to think that 2+2 is a "literal". The
                    > standard has a perfectly good term for the concept we're discussing,
                    > "constant expression".
                    >[/color]

                    Imagine my surprise..

                    /* Constant Literal Expression? */
                    #include <stdio.h>
                    int main(void) {
                    int i;
                    for (i = 0; i < 4; ++i) {
                    switch(i+1) {
                    case 1: puts("case 1"); break;
                    case 1+1: puts("case 2"); break;
                    case 1+1+1: puts("case 3"); break;
                    case 2+2: puts("case 4"); break;
                    }
                    }
                    return 0;
                    }

                    ...prints

                    case 1
                    case 2
                    case 3
                    case 4

                    --
                    Joe Wright mailto:joewwrig ht@comcast.net
                    "Everything should be made as simple as possible, but not simpler."
                    --- Albert Einstein ---

                    Comment

                    • lonny.nettnay@gmail.com

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

                      I'm using Google Groups. It must have dropped the space.
                      Still liked the program. :)

                      Lonny

                      Comment

                      • Lawrence Kirby

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

                        On Thu, 30 Dec 2004 01:07:17 +0000, Mark McIntyre wrote:

                        ....
                        [color=blue][color=green]
                        >>Everything that counts as a constant expression from
                        >> 1
                        >>over
                        >> 3<<2
                        >>to
                        >> (7+5)/6*4[/color]
                        >
                        > Er, these are all literals (as far as I'm concerned). I suspect we have a
                        > nomenclature disagreement.[/color]

                        I can understand why you might call something like 5 or 'X' a literal (the
                        standard calls them constants). Given that 3<<2 is an expression that
                        happens to contain 2 literals. I cna see no sense in which the overall
                        expression could be called a literal, unless you are using the term
                        "literal" to mean "constant expression". But that is a non-obvious
                        association.

                        Lawrence

                        Comment

                        • Mark McIntyre

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

                          On Wed, 29 Dec 2004 17:16: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 Wed, 29 Dec 2004 16:23:08 +0100, in comp.lang.c , Michael Mair
                          >> <Michael.Mair@i nvalid.invalid> wrote:[color=darkred]
                          >>>Everything that counts as a constant expression from
                          >>> 1
                          >>>over
                          >>> 3<<2
                          >>>to
                          >>> (7+5)/6*4[/color]
                          >>
                          >> Er, these are all literals (as far as I'm concerned). I suspect we have a
                          >> nomenclature disagreement.[/color]
                          >
                          >You'll have to define "literal" for the rest of us then. The
                          >standard only uses it in the phrases "string literal" and
                          >"compound literal".[/color]

                          So? Last time I looked, the Standard wasn't a dictionary of computing
                          terms.

                          FWIW as far as I think of it, a literal value is one typed in, literally.

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

                          • Keith Thompson

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

                            Mark McIntyre <markmcintyre@s pamcop.net> writes:[color=blue]
                            > On Wed, 29 Dec 2004 17:16:26 -0800, in comp.lang.c , Ben Pfaff
                            > <blp@cs.stanfor d.edu> wrote:[color=green]
                            >>Mark McIntyre <markmcintyre@s pamcop.net> writes:[color=darkred]
                            >>> On Wed, 29 Dec 2004 16:23:08 +0100, in comp.lang.c , Michael Mair
                            >>> <Michael.Mair@i nvalid.invalid> wrote:
                            >>>>Everythin g that counts as a constant expression from
                            >>>> 1
                            >>>>over
                            >>>> 3<<2
                            >>>>to
                            >>>> (7+5)/6*4
                            >>>
                            >>> Er, these are all literals (as far as I'm concerned). I suspect we have a
                            >>> nomenclature disagreement.[/color]
                            >>
                            >>You'll have to define "literal" for the rest of us then. The
                            >>standard only uses it in the phrases "string literal" and
                            >>"compound literal".[/color]
                            >
                            > So? Last time I looked, the Standard wasn't a dictionary of computing
                            > terms.[/color]

                            No, it isn't, but there are plenty of dictionaries of computing terms.
                            Do you know of one that defines "literal" in a manner consistent with
                            your usage?
                            [color=blue]
                            > FWIW as far as I think of it, a literal value is one typed in, literally.[/color]

                            I don't see how that makes 3<<2 a literal and x+3 not a literal. In
                            any case, you're using the term in a way that nobody else does. I'm
                            not absolutely saying that you're wrong and everyone else is right,
                            but assuming that you're right and everyone else is wrong is not
                            likely to help communication, which is after all the point of this
                            newsgroup.

                            Does your concept of "literal" match what the C standard calls a
                            "constant expression"? If so, perhaps we could just all agree to call
                            them "constant expressions" and move on.

                            --
                            Keith Thompson (The_Other_Keit h) 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.

                            Comment

                            • dcorbit@connx.com

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

                              If you know what all the cases are, you can do a perfect hash for the
                              input strings and then switch on the hash.

                              This is the C-FAQ version:

                              20.17: Is there a way to switch on strings?

                              A: Not directly. Sometimes, it's appropriate to use a separate
                              function to map strings to integer codes, and then switch on
                              those. Otherwise, of course, you can fall back on strcmp() and
                              a conventional if/else chain. See also questions 10.12, 20.18,
                              and 20.29.

                              References: K&R1 Sec. 3.4 p. 55; K&R2 Sec. 3.4 p. 58; ISO
                              Sec. 6.6.4.2; H&S Sec. 8.7 p. 248.

                              Comment

                              • Mark McIntyre

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

                                On Fri, 31 Dec 2004 02:17:28 GMT, in comp.lang.c , Keith Thompson
                                <kst-u@mib.org> wrote:
                                [color=blue]
                                >Mark McIntyre <markmcintyre@s pamcop.net> writes:[/color]
                                [color=blue][color=green]
                                >> FWIW as far as I think of it, a literal value is one typed in, literally.[/color]
                                >
                                >I don't see how that makes 3<<2 a literal and x+3 not a literal.[/color]

                                x isn't a value. Its a symbol.
                                [color=blue]
                                >In any case, you're using the term in a way that nobody else does.[/color]

                                I should have defined my meaning in my original post. My mistake.


                                --
                                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...