Is that a constant expression?

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

    Is that a constant expression?

    Assume we have somewhere
    typedef enum foo { A,B,C } foo;

    Is C+1 a constant expression?

    [
    I want to write smth. like
    char* strs[C+1]={"Astr","Bstr" ,"Cstr"};
    ]

    Could anyone help?
    Regards,
    Alex.
    --
    comp.lang.c.mod erated - moderation address: clcm@plethora.n et
  • Mike Wahler

    #2
    Re: Is that a constant expression?


    "Alexander Malkis" <alexmalk@LOESC HEDIESline.cs.u ni-sb.de> wrote in message
    news:clcm-20040226-0017@plethora.n et...[color=blue]
    > Assume we have somewhere
    > typedef enum foo { A,B,C } foo;
    >
    > Is C+1 a constant expression?[/color]

    Yes.
    [color=blue]
    >
    > [
    > I want to write smth. like
    > char* strs[C+1]={"Astr","Bstr" ,"Cstr"};
    > ]
    >
    > Could anyone help?[/color]

    It looks OK to me. Are you having trouble with it?

    -Mike



    Comment

    • Alex

      #3
      Re: Is that a constant expression?

      No. Not yet.
      Thank you.
      Alex.

      Mike Wahler wrote:[color=blue]
      >
      > "Alexander Malkis" <alexmalk@LOESC HEDIESline.cs.u ni-sb.de> wrote in message
      > news:clcm-20040226-0017@plethora.n et...[color=green]
      > > Assume we have somewhere
      > > typedef enum foo { A,B,C } foo;
      > >
      > > Is C+1 a constant expression?[/color]
      >
      > Yes.
      >[color=green]
      > >
      > > [
      > > I want to write smth. like
      > > char* strs[C+1]={"Astr","Bstr" ,"Cstr"};
      > > ]
      > >
      > > Could anyone help?[/color]
      >
      > It looks OK to me. Are you having trouble with it?
      >
      > -Mike[/color]

      Comment

      • Dag Henriksson

        #4
        Re: Is that a constant expression?


        "Alexander Malkis" <alexmalk@LOESC HEDIESline.cs.u ni-sb.de> skrev i
        meddelandet news:clcm-20040226-0017@plethora.n et...[color=blue]
        > Assume we have somewhere
        > typedef enum foo { A,B,C } foo;
        >
        > Is C+1 a constant expression?
        >[/color]

        That depends.
        If the built-in operator+ is used, it is. If you have written an own
        operator+(foo, int), it isn't.

        --
        Dag Henriksson


        Comment

        • Leor Zolman

          #5
          Re: Is that a constant expression?

          On 26 Feb 2004 21:56:14 GMT, Alexander Malkis
          <alexmalk@LOESC HEDIESline.cs.u ni-sb.de> wrote:
          [color=blue]
          >Assume we have somewhere
          >typedef enum foo { A,B,C } foo;
          >
          >Is C+1 a constant expression?[/color]

          Yes.
          [color=blue]
          >
          >[
          >I want to write smth. like
          >char* strs[C+1]={"Astr","Bstr" ,"Cstr"};
          >]
          >
          >Could anyone help?[/color]

          Well, you start up your text editor, and then type it in ;-)

          Seriously, though, wouldn't just trying this in your compiler have been
          less work than entering the names of all those newsgroups you've
          cross-posted to?

          [color=blue]
          >Regards,
          >Alex.[/color]

          Leor Zolman
          BD Software
          leor@bdsoft.com
          www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
          C++ users: Download BD Software's free STL Error Message
          Decryptor at www.bdsoft.com/tools/stlfilt.html
          --
          comp.lang.c.mod erated - moderation address: clcm@plethora.n et

          Comment

          • Thomas Matthews

            #6
            Re: Is that a constant expression?

            Alexander Malkis wrote:[color=blue]
            > Assume we have somewhere
            > typedef enum foo { A,B,C } foo;
            >
            > Is C+1 a constant expression?
            >
            > [
            > I want to write smth. like
            > char* strs[C+1]={"Astr","Bstr" ,"Cstr"};
            > ]
            >
            > Could anyone help?
            > Regards,
            > Alex.[/color]

            What's wrong with letting the compiler figure
            out the number of elements?

            char * strs[] = {"Astr", "Bstr", "Cstr"};

            --
            Thomas Matthews

            C++ newsgroup welcome message:

            C++ Faq: http://www.parashift.com/c++-faq-lite
            C Faq: http://www.eskimo.com/~scs/c-faq/top.html
            alt.comp.lang.l earn.c-c++ faq:

            Other sites:
            http://www.josuttis.com -- C++ STL Library book
            http://www.sgi.com/tech/stl -- Standard Template Library
            --
            comp.lang.c.mod erated - moderation address: clcm@plethora.n et

            Comment

            • Jeff Schwab

              #7
              Re: Is that a constant expression?

              Alexander Malkis wrote:
              [color=blue]
              > typedef enum foo { A,B,C } foo;[/color]

              .....
              [color=blue]
              > char* strs[C+1]={"Astr","Bstr" ,"Cstr"};[/color]


              NB: The following refers to C++. I don't know the answer in C.

              Your approach almost certainly will work in this particular case.
              However, it may not work in the general case. Enums are not guaranteed
              to have any minimum number of bits, as long as they are wide enough to
              hold all of their enumerators.

              Here is a common alternative:

              enum Foo { a, b, c, num_foo };

              -Jeff
              --
              comp.lang.c.mod erated - moderation address: clcm@plethora.n et

              Comment

              • Hans-Bernhard Broeker

                #8
                Re: Is that a constant expression?

                [F'up2 cut down --- should have been done by OP!]

                In comp.lang.c.mod erated Alexander Malkis wrote:
                [color=blue]
                > Assume we have somewhere
                > typedef enum foo { A,B,C } foo;[/color]
                [color=blue]
                > Is C+1 a constant expression?[/color]

                Yes. What makes you suspect otherwise?
                [color=blue]
                > I want to write smth. like
                > char* strs[C+1]={"Astr","Bstr" ,"Cstr"};[/color]

                You don't need C+1 to be a constant, for that. Actually, you don't
                need C+1 at all:

                char *strs[] = {"Astr", "Bstr", "Cstr"};


                --
                Hans-Bernhard Broeker (broeker@physik .rwth-aachen.de)
                Even if all the snow were burnt, ashes would remain.
                --
                comp.lang.c.mod erated - moderation address: clcm@plethora.n et

                Comment

                • Dan Pop

                  #9
                  Re: Is that a constant expression?

                  In <clcm-20040226-0017@plethora.n et> Alexander Malkis <alexmalk@LOESC HEDIESline.cs.u ni-sb.de> writes:
                  [color=blue]
                  >Assume we have somewhere
                  >typedef enum foo { A,B,C } foo;
                  >
                  >Is C+1 a constant expression?[/color]

                  Yes.
                  [color=blue]
                  >[
                  >I want to write smth. like
                  >char* strs[C+1]={"Astr","Bstr" ,"Cstr"};
                  >][/color]

                  What's wrong with:

                  char *strs[] = {"Astr", "Bstr", "Cstr"};

                  ?

                  Dan
                  --
                  Dan Pop
                  DESY Zeuthen, RZ group
                  Email: Dan.Pop@ifh.de
                  --
                  comp.lang.c.mod erated - moderation address: clcm@plethora.n et

                  Comment

                  • those who know me have no need of my name

                    #10
                    Re: Is that a constant expression?

                    [ugh -- horribly cross-posted and no followup-to]

                    in comp.lang.c.mod erated i read:
                    [color=blue]
                    >Assume we have somewhere
                    >typedef enum foo { A,B,C } foo;
                    >
                    >Is C+1 a constant expression?[/color]

                    yes.

                    --
                    a signature
                    --
                    comp.lang.c.mod erated - moderation address: clcm@plethora.n et

                    Comment

                    • Claus Reibenstein

                      #11
                      Re: Is that a constant expression?

                      Alexander Malkis schrieb:
                      [color=blue]
                      > Is C+1 a constant expression?[/color]

                      Where do you want to find the answer? I see no fup2. And obviously
                      no-one so far recognized your crossposting anyway. Poor blind guys.

                      Fup2 ... hmmm ... where? ... let's see ... yes, that seems adequate.

                      Claus
                      --
                      comp.lang.c.mod erated - moderation address: clcm@plethora.n et

                      Comment

                      • Tor Husabø

                        #12
                        Re: Is that a constant expression?

                        Jeff Schwab wrote:
                        [color=blue]
                        > Alexander Malkis wrote:
                        >[color=green]
                        >> typedef enum foo { A,B,C } foo;[/color]
                        >
                        >
                        > ....
                        >[color=green]
                        >> char* strs[C+1]={"Astr","Bstr" ,"Cstr"};[/color]
                        >
                        >
                        >
                        > NB: The following refers to C++. I don't know the answer in C.
                        >
                        > Your approach almost certainly will work in this particular case.
                        > However, it may not work in the general case. Enums are not guaranteed
                        > to have any minimum number of bits, as long as they are wide enough to
                        > hold all of their enumerators.[/color]

                        Just for the record, an enum in C, unlike in C++, is always an int. And
                        the size enum foo doesn't matter here anyway.

                        Tor
                        --
                        comp.lang.c.mod erated - moderation address: clcm@plethora.n et

                        Comment

                        • Jeff Schwab

                          #13
                          Re: Is that a constant expression?

                          Tor Husabø wrote:[color=blue]
                          > Jeff Schwab wrote:
                          >[color=green]
                          >> Alexander Malkis wrote:
                          >>[color=darkred]
                          >>> typedef enum foo { A,B,C } foo;[/color]
                          >>
                          >>
                          >>
                          >> ....
                          >>[color=darkred]
                          >>> char* strs[C+1]={"Astr","Bstr" ,"Cstr"};[/color]
                          >>
                          >>
                          >> NB: The following refers to C++. I don't know the answer in C.
                          >>
                          >> Your approach almost certainly will work in this particular case.
                          >> However, it may not work in the general case. Enums are not
                          >> guaranteed to have any minimum number of bits, as long as they are
                          >> wide enough to hold all of their enumerators.[/color]
                          >
                          >
                          > Just for the record, an enum in C, unlike in C++, is always an int. And
                          > the size enum foo doesn't matter here anyway.[/color]

                          Yes, you're right. Of course, if A, B, and C were macros representing
                          expressions with large initializers, or if int's were only one bit
                          wide... :)

                          It is true, though, that the size of the array doesn't need to be
                          specified explicitly here anyway.
                          --
                          comp.lang.c.mod erated - moderation address: clcm@plethora.n et

                          Comment

                          Working...