a basic question,need help

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

    a basic question,need help

    i know that in c plus plus,++x returns l-value while x++ returns r-
    value,but what is the situation in c,are both ++x and x++ return r-
    value? i don't know how C99 defines it,thx.
  • Eric Sosman

    #2
    Re: a basic question,need help

    jackie wrote:
    i know that in c plus plus,++x returns l-value while x++ returns r-
    value,but what is the situation in c,are both ++x and x++ return r-
    value? i don't know how C99 defines it,thx.
    In C (all versions), neither ++x nor x++ is an lvalue.
    I'm surprised to hear that this is different in C++, because
    it makes no sense to me. Is

    int x = 42;
    ++x = 97;

    legal in C++? If so, what is the value of x afterwards?
    (Actually, you can disregard the second question: If the
    answer to the first is anything other than "No," I don't
    want to know anything more about C++.)

    --
    Eric Sosman
    esosman@ieee-dot-org.invalid

    Comment

    • Tommy

      #3
      Re: a basic question,need help

      [cuitao@mytest c]$ vi pp.c

      #include <stdio.h>
      int main()
      {
      int x=10;
      x=20;
      printf("\nx=%d\ n",x);

      return 0;
      }
      ~
      ~
      ~
      "pp.c" 9L, 99C written
      [cuitao@mytest c]$
      [cuitao@mytest c]$
      [cuitao@mytest c]$ cc ./pp.c -o ./pp
      [cuitao@mytest c]$
      [cuitao@mytest c]$ ./pp

      x=20
      [cuitao@mytest c]$


      [cuitao@mytest c]$ vi pp.c

      #include <stdio.h>
      int main()
      {
      int x=10;
      x++=20;
      printf("\nx=%d\ n",x);

      return 0;
      }
      ~
      ~
      ~
      "pp.c" 9L, 101C written
      [cuitao@mytest c]$
      [cuitao@mytest c]$
      [cuitao@mytest c]$ cc ./pp.c -o ./pp
      ../pp.c: In function `main':
      ../pp.c:5: error: invalid lvalue in assignment





      [cuitao@mytest c]$ vi pp.c

      #include <stdio.h>
      int main()
      {
      int x=10;
      ++x=20;
      printf("\nx=%d\ n",x);

      return 0;
      }
      ~
      ~
      ~
      "pp.c" 9L, 101C written
      [cuitao@mytest c]$ cc ./pp.c -o ./pp
      ../pp.c: In function `main':
      ../pp.c:5: error: invalid lvalue in assignment
      [cuitao@mytest c]$







      "Eric Sosman" <esosman@ieee-dot-org.invalid>
      ??????:4sadnefS S4WSyT_VnZ2dnUV Z_jOdnZ2d@comca st.com...
      jackie wrote:
      >i know that in c plus plus,++x returns l-value while x++ returns r-
      >value,but what is the situation in c,are both ++x and x++ return r-
      >value? i don't know how C99 defines it,thx.
      >
      In C (all versions), neither ++x nor x++ is an lvalue.
      I'm surprised to hear that this is different in C++, because
      it makes no sense to me. Is
      >
      int x = 42;
      ++x = 97;
      >
      legal in C++? If so, what is the value of x afterwards?
      (Actually, you can disregard the second question: If the
      answer to the first is anything other than "No," I don't
      want to know anything more about C++.)
      >
      --
      Eric Sosman
      esosman@ieee-dot-org.invalid

      Comment

      • Ian Collins

        #4
        Re: a basic question,need help

        Eric Sosman wrote:
        jackie wrote:
        >i know that in c plus plus,++x returns l-value while x++ returns r-
        >value,but what is the situation in c,are both ++x and x++ return r-
        >value? i don't know how C99 defines it,thx.
        >
        In C (all versions), neither ++x nor x++ is an lvalue.
        I'm surprised to hear that this is different in C++, because
        it makes no sense to me. Is
        >
        int x = 42;
        ++x = 97;
        >
        legal in C++?
        No.

        --
        Ian Collins.

        Comment

        • CBFalconer

          #5
          Re: a basic question,need help

          jackie wrote:
          >
          i know that in c plus plus,++x returns l-value while x++ returns
          r- value,but what is the situation in c,are both ++x and x++
          return r- value? i don't know how C99 defines it,thx.
          You're wrong. Illustrative snippet:

          volatile int x = 123;
          int y;

          y = x;
          if ((y == x++) && ((y + 1) == x)) puts("Correct x++");
          else puts("Bad system");

          y = x;
          if (((y + 1) == ++x) && ((y + 1) == x)) puts("Correct ++x");
          else puts("Bad system");

          The operator has nothing to to with lvalues and rvalues. Same in
          C++. Note that in the snippet the first phrase in the test (before
          the &&) is completed before the 2nd phase is begun.

          --
          [mail]: Chuck F (cbfalconer at maineline dot net)
          [page]: <http://cbfalconer.home .att.net>
          Try the download section.

          Comment

          • Daniel Pitts

            #6
            Re: a basic question,need help

            Ian Collins wrote:
            Eric Sosman wrote:
            >jackie wrote:
            >>i know that in c plus plus,++x returns l-value while x++ returns r-
            >>value,but what is the situation in c,are both ++x and x++ return r-
            >>value? i don't know how C99 defines it,thx.
            > In C (all versions), neither ++x nor x++ is an lvalue.
            >I'm surprised to hear that this is different in C++, because
            >it makes no sense to me. Is
            >>
            > int x = 42;
            > ++x = 97;
            >>
            >legal in C++?
            >
            No.
            >
            Sure it is:

            class foo {
            public:
            foo(int x) {
            }
            foo &operator=(i nt x){}
            };
            foo &operator++( foo &x) {
            return x;
            }

            int main() {
            #define int foo
            int x = 72;
            ++x = 32;
            }

            --
            Daniel Pitts' Tech Blog: <http://virtualinfinity .net/wordpress/>

            Comment

            • Eric Schmidt

              #7
              Re: a basic question,need help

              Eric Sosman wrote:
              jackie wrote:
              >
              >i know that in c plus plus,++x returns l-value while x++ returns r-
              >value,but what is the situation in c,are both ++x and x++ return r-
              >value? i don't know how C99 defines it,thx.
              >
              >
              In C (all versions), neither ++x nor x++ is an lvalue.
              I'm surprised to hear that this is different in C++, because
              it makes no sense to me. Is
              >
              int x = 42;
              ++x = 97;
              >
              legal in C++? If so, what is the value of x afterwards?
              (Actually, you can disregard the second question: If the
              answer to the first is anything other than "No," I don't
              want to know anything more about C++.)
              >
              It yields undefined behavior in C++ because x is modified twice without
              an intervening sequence point. But something like &(++x) is legal
              (though it seems useless to me).
              ** Posted from http://www.teranews.com **

              Comment

              • Keith Thompson

                #8
                Re: a basic question,need help

                CBFalconer <cbfalconer@yah oo.comwrites:
                jackie wrote:
                >>
                >i know that in c plus plus,++x returns l-value while x++ returns
                >r- value,but what is the situation in c,are both ++x and x++
                >return r- value? i don't know how C99 defines it,thx.
                >
                You're wrong.
                Just what is it that you think he's wrong about?

                (I'm assuming jackie is a "he"; apologies if I'm mistaken.)

                In C++, the result of ``++x'' is an lvalue, and the result of ``x++''
                is not an lvalue, so he's right about that; it's off-topic, but a
                perfectly reasonable introduction to his question.

                The rest of the post was a question, which can hardly be wrong, and a
                statement that he doesn't know something, which you can hardly assume
                is wrong.
                Illustrative snippet:
                Illustrative of what?
                volatile int x = 123;
                int y;
                >
                y = x;
                if ((y == x++) && ((y + 1) == x)) puts("Correct x++");
                else puts("Bad system");
                >
                y = x;
                if (((y + 1) == ++x) && ((y + 1) == x)) puts("Correct ++x");
                else puts("Bad system");
                What is the point of declaring x volatile?
                The operator has nothing to to with lvalues and rvalues. Same in
                C++. Note that in the snippet the first phrase in the test (before
                the &&) is completed before the 2nd phase is begun.
                Certainly it does.

                In both C and C++, the operand of a prefix or postfix "++" must be a
                modifiable lvalue. In C, the result of either operator is not an
                lvalue; in C++, one is an lvalue and one isn't.

                In any case, your code snippet is equally valid in either C or C++,
                and would behave in exactly the same way even if both prefix and
                postfix "++" yielded lvalues; you never use them in a context that
                requires an lvalue.

                So what was your point again?

                --
                Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                Nokia
                "We must do something. This is something. Therefore, we must do this."
                -- Antony Jay and Jonathan Lynn, "Yes Minister"

                Comment

                • jackie

                  #9
                  Re: a basic question,need help

                  On Aug 13, 3:29 pm, Keith Thompson <ks...@mib.orgw rote:
                  CBFalconer <cbfalco...@yah oo.comwrites:
                  jackie wrote:
                  >
                  i know that in c plus plus,++x returns l-value while x++ returns
                  r- value,but what is the situation in c,are both ++x and x++
                  return r- value? i don't know how C99 defines it,thx.
                  >
                  You're wrong.
                  >
                  Just what is it that you think he's wrong about?
                  >
                  (I'm assuming jackie is a "he"; apologies if I'm mistaken.)
                  >
                  In C++, the result of ``++x'' is an lvalue, and the result of ``x++''
                  is not an lvalue, so he's right about that; it's off-topic, but a
                  perfectly reasonable introduction to his question.
                  >
                  The rest of the post was a question, which can hardly be wrong, and a
                  statement that he doesn't know something, which you can hardly assume
                  is wrong.
                  >
                                 Illustrative snippet:
                  >
                  Illustrative of what?
                  >
                      volatile int x = 123;
                      int          y;
                  >
                      y = x;
                      if ((y == x++) && ((y + 1) == x)) puts("Correct x++");
                      else                              puts("Bad system");
                  >
                      y = x;
                      if (((y + 1) == ++x) && ((y + 1) == x)) puts("Correct ++x");
                      else                                    puts("Bad system");
                  >
                  What is the point of declaring x volatile?
                  >
                  The operator has nothing to to with lvalues and rvalues.  Same in
                  C++.  Note that in the snippet the first phrase in the test (before
                  the &&) is completed before the 2nd phase is begun.
                  >
                  Certainly it does.
                  >
                  In both C and C++, the operand of a prefix or postfix "++" must be a
                  modifiable lvalue.  In C, the result of either operator is not an
                  lvalue; in C++, one is an lvalue and one isn't.
                  >
                  In any case, your code snippet is equally valid in either C or C++,
                  and would behave in exactly the same way even if both prefix and
                  postfix "++" yielded lvalues; you never use them in a context that
                  requires an lvalue.
                  >
                  So what was your point again?
                  >
                  --
                  Keith Thompson (The_Other_Keit h) ks...@mib.org  <http://www.ghoti.net/~kst>
                  Nokia
                  "We must do something.  This is something.  Therefore, we must do this."
                      -- Antony Jay and Jonathan Lynn, "Yes Minister"


                  u're right,i'm a "he". ^-^
                  and i think u have already give me ur answer,that's both ++x and x++
                  return r-value in c,do u mean that? thx for ur help

                  Comment

                  • CBFalconer

                    #10
                    Re: a basic question,need help

                    Keith Thompson wrote:
                    CBFalconer <cbfalconer@yah oo.comwrites:
                    >
                    .... snip ...
                    >
                    >Illustrative snippet:
                    >
                    Illustrative of what?
                    Of the behaviour of x++ and ++x.
                    >
                    > volatile int x = 123;
                    > int y;
                    >>
                    > y = x;
                    > if ((y == x++) && ((y + 1) == x)) puts("Correct x++");
                    > else puts("Bad system");
                    >>
                    > y = x;
                    > if (((y + 1) == ++x) && ((y + 1) == x)) puts("Correct ++x");
                    > else puts("Bad system");
                    >
                    What is the point of declaring x volatile?
                    That ensures that x is accessed for each equality test. That way
                    it can't be carried along in a working register.
                    >
                    >The operator has nothing to do with lvalues and rvalues. Same in
                    >C++. Note that in the snippet the first phrase in the test (before
                    >the &&) is completed before the 2nd phase is begun.
                    >
                    Certainly it does.
                    >
                    In both C and C++, the operand of a prefix or postfix "++" must be a
                    modifiable lvalue. In C, the result of either operator is not an
                    lvalue; in C++, one is an lvalue and one isn't.
                    >
                    In any case, your code snippet is equally valid in either C or C++,
                    and would behave in exactly the same way even if both prefix and
                    postfix "++" yielded lvalues; you never use them in a context that
                    requires an lvalue.
                    >
                    So what was your point again?
                    To demonstrate the actions of x++ and ++x, and their difference.

                    --
                    [mail]: Chuck F (cbfalconer at maineline dot net)
                    [page]: <http://cbfalconer.home .att.net>
                    Try the download section.


                    Comment

                    • James Kuyper

                      #11
                      Re: a basic question,need help

                      CBFalconer wrote:
                      Keith Thompson wrote:
                      >CBFalconer <cbfalconer@yah oo.comwrites:
                      >>
                      ... snip ...
                      >>Illustrativ e snippet:
                      >Illustrative of what?
                      >
                      Of the behaviour of x++ and ++x.
                      >> volatile int x = 123;
                      >> int y;
                      >>>
                      >> y = x;
                      >> if ((y == x++) && ((y + 1) == x)) puts("Correct x++");
                      >> else puts("Bad system");
                      >>>
                      >> y = x;
                      >> if (((y + 1) == ++x) && ((y + 1) == x)) puts("Correct ++x");
                      >> else puts("Bad system");
                      ....
                      >In any case, your code snippet is equally valid in either C or C++,
                      >and would behave in exactly the same way even if both prefix and
                      >postfix "++" yielded lvalues; you never use them in a context that
                      >requires an lvalue.
                      >>
                      >So what was your point again?
                      >
                      To demonstrate the actions of x++ and ++x, and their difference.
                      The question was about the lvalue-ness of x++ and ++x; what does your
                      example have to do with that question?

                      Comment

                      • CBFalconer

                        #12
                        Re: a basic question,need help

                        James Kuyper wrote:
                        CBFalconer wrote:
                        >Keith Thompson wrote:
                        >>CBFalconer <cbfalconer@yah oo.comwrites:
                        >>>
                        >... snip ...
                        >>
                        >>>Illustrati ve snippet:
                        >>Illustrativ e of what?
                        >>
                        >Of the behaviour of x++ and ++x.
                        >>>
                        >>> volatile int x = 123;
                        >>> int y;
                        >>>>
                        >>> y = x;
                        >>> if ((y == x++) && ((y + 1) == x)) puts("Correct x++");
                        >>> else puts("Bad system");
                        >>>>
                        >>> y = x;
                        >>> if (((y + 1) == ++x) && ((y + 1) == x)) puts("Correct ++x");
                        >>> else puts("Bad system");
                        ...
                        >>In any case, your code snippet is equally valid in either C or
                        >>C++, and would behave in exactly the same way even if both
                        >>prefix and postfix "++" yielded lvalues; you never use them in
                        >>a context that requires an lvalue.
                        >>>
                        >>So what was your point again?
                        >>
                        >To demonstrate the actions of x++ and ++x, and their difference.
                        >
                        The question was about the lvalue-ness of x++ and ++x; what does
                        your example have to do with that question?
                        Well, for one thing you can see immediately that x++ can't have an
                        lvalue. There is nothing holding that value, apart from the
                        reference y.

                        --
                        [mail]: Chuck F (cbfalconer at maineline dot net)
                        [page]: <http://cbfalconer.home .att.net>
                        Try the download section.


                        Comment

                        • christian.bau

                          #13
                          Re: a basic question,need help

                          On Aug 13, 4:25 am, Eric Sosman <esos...@ieee-dot-org.invalidwrot e:
                          I'm surprised to hear that this is different in C++, because
                          it makes no sense to me. Is
                          >
                          int x = 42;
                          ++x = 97;
                          >
                          legal in C++? If so, what is the value of x afterwards?
                          (Actually, you can disregard the second question: If the
                          answer to the first is anything other than "No," I don't
                          want to know anything more about C++.)
                          It is syntactically legal, but it invokes undefined behaviour same as
                          it would in C if ++x was an lvalue (the same object is modified twice
                          without intervening sequence point). Something that is legal and makes
                          a bit of sense would be:

                          int x = 42;
                          int *p = &++x;

                          This will give you x = 43, and p = &x. There is motivation for this
                          because of "references " in C++. If you have a C++ function

                          int f (int& x) { return x++; }

                          then you can call

                          int x = 42;
                          int y = f (++x);

                          which will increase x by one, pass x "by reference" to the function f
                          which is rather the same as passing the address; f will increase x in
                          the caller (because it has a reference to it) and return the value
                          before the incremenent. Effectively this will set x to 44 and y to 43,
                          and there is no undefined behavior because there are plenty of
                          sequence points.

                          Comment

                          • Keith Thompson

                            #14
                            Re: a basic question,need help

                            jackie <jackiexxm@gmai l.comwrites:
                            [...]
                            u're right,i'm a "he". ^-^
                            and i think u have already give me ur answer,that's both ++x and x++
                            return r-value in c,do u mean that? thx for ur help
                            Yes, both ++x and x++ yield a non-lvalue in C. (You can call that an
                            "rvalue", but C doesn't use the term except in a single footnote.)

                            Incidentally, silly abbreviations like "u", "ur", and "thx" are
                            frowwned upon here. Please take the time to spell out simple words
                            like "you", "your", and "thanks". Not everyone here speaks English as
                            a native language -- and those of us who do generally prefer to read
                            actual English.

                            Also, when you post a followup, it's rarely necessary to quote the
                            entire parent article. Delete any quoted material that's not relevant
                            to your followup, leaving just enough so that your article makes sense
                            to someone who hasn't seen the parent. In particular, don't quote
                            signatures unless you're actually commenting on them.

                            Thx. 8-)}

                            --
                            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                            Nokia
                            "We must do something. This is something. Therefore, we must do this."
                            -- Antony Jay and Jonathan Lynn, "Yes Minister"

                            Comment

                            • jameskuyper@verizon.net

                              #15
                              Re: a basic question,need help

                              CBFalconer wrote:
                              James Kuyper wrote:
                              CBFalconer wrote:
                              Keith Thompson wrote:
                              >CBFalconer <cbfalconer@yah oo.comwrites:
                              >>
                              ... snip ...
                              >
                              >>Illustrativ e snippet:
                              >Illustrative of what?
                              >
                              Of the behaviour of x++ and ++x.
                              >>
                              >> volatile int x = 123;
                              >> int y;
                              >>>
                              >> y = x;
                              >> if ((y == x++) && ((y + 1) == x)) puts("Correct x++");
                              >> else puts("Bad system");
                              >>>
                              >> y = x;
                              >> if (((y + 1) == ++x) && ((y + 1) == x)) puts("Correct ++x");
                              >> else puts("Bad system");
                              ...
                              >In any case, your code snippet is equally valid in either C or
                              >C++, and would behave in exactly the same way even if both
                              >prefix and postfix "++" yielded lvalues; you never use them in
                              >a context that requires an lvalue.
                              >>
                              >So what was your point again?
                              >
                              To demonstrate the actions of x++ and ++x, and their difference.
                              The question was about the lvalue-ness of x++ and ++x; what does
                              your example have to do with that question?
                              >
                              Well, for one thing you can see immediately that x++ can't have an
                              lvalue. There is nothing holding that value, apart from the
                              reference y.
                              Actually, it is ++x, not x++, which is an lvalue in C++, and your code
                              does nothing to demonstrate that "There is nothing holding" the value
                              of ++x, because you code makes no use of ++x in a context that
                              requires an lvalue.
                              The example provided by Daniel Pits does use ++x in a context that
                              requires an lvalue. The fact that his code compiles and works as he
                              expected it to work demonstrates that jackie was in fact correct in
                              saying that ++x is an lvalue in C++, your assertions to the contrary
                              notwithstanding .

                              Actually, as a demonstration, Daniel's code could be improved upon. It
                              would be clearer what the lvalued-ness of ++x means in C++, if his foo
                              class had contained a non-static data member that participated in
                              operator++() and operator=() in a non-trivial fashion. I assure you
                              that adding such a data member would not prevent the code from
                              working. If he had done so, it would have been quite clear that there
                              is indeed something "holding that value". The "something" is what C++
                              calls an temporary object.

                              The fact that C cannot usefully make ++x an lvalue is due the fact
                              that C structs cannot have member functions. The fact that C has no
                              need to make ++x an lvalue is mainly due to the fact that C doesn't
                              have operator overloads. It's not because it would be impossible to
                              make ++x an lvalue in C; it would have been just as feasible for C to
                              introduce the concept of a temporary object.

                              Comment

                              Working...