operator precedance

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

    operator precedance

    I am having difficulty understanding this piece of code. I don't
    understand why ++*ptr prints 7 and *ptr-- prints an unknown value. I
    would have expected it to print 8. I would have to put brackets (*ptr)--
    for it work. Thanx in advance.

    Joseph

    #include <stdio.h>

    int main(void)
    {
    int ctr =6;
    int *ptr=&ctr;

    ++*ptr;
    printf("ptr=%d\ n", *ptr); /*prints 7*/

    *ptr++;
    printf("ptr=%d\ n", *ptr); /* prints ? */
    }

  • Eric Sosman

    #2
    Re: operator precedance

    Joseph wrote:[color=blue]
    >
    > I am having difficulty understanding this piece of code. I don't
    > understand why ++*ptr prints 7 and *ptr-- prints an unknown value. I
    > would have expected it to print 8. I would have to put brackets (*ptr)--
    > for it work. Thanx in advance.
    >
    > #include <stdio.h>
    >
    > int main(void)
    > {
    > int ctr =6;
    > int *ptr=&ctr;
    >
    > ++*ptr;
    > printf("ptr=%d\ n", *ptr); /*prints 7*/[/color]

    Right. `++*ptr' is equivalent to `++(*ptr)', which
    is equivalent to `++(ctr)', which is equivalent to `++ctr'.
    Since `ctr' starts out with the value six, `++ctr' changes
    it to seven and yields seven as the value (which is then
    discarded). `ptr' itself is not changed, so when you get
    to the printf() call `*ptr' is still equivalent to `ctr',
    which now has the value seven.
    [color=blue]
    > *ptr++;
    > printf("ptr=%d\ n", *ptr); /* prints ? */[/color]

    Might print anything at all, or nothing, or make demons
    fly from your nostrils. Let's go through it: `*ptr++' is
    equivalent to `*(ptr++)'. The `ptr++' piece does two things:
    it increments `ptr' and yields the value `ptr' had prior to
    being incremented. So `*(ptr++)' is equivalent to `(ctr)'
    plus the side-effect of incrementing `ptr'. Now when you
    get to the printf() call, `ptr' no longer points to `ctr'
    because it has been incremented; it points to something "one
    slot past the end" of `ctr'. There may or may not actually
    be anything at this spot; if something does dwell there it
    may or may not be an `int'. You are sticking your hand into
    a sack without any idea what's inside: it might be an `int',
    or it might be a piece of salt-water taffy, or it might be
    an enraged cobra. Don't Do That.

    --
    Eric.Sosman@sun .com

    Comment

    • Mark A. Odell

      #3
      Re: operator precedance

      Joseph <kermit24@roger s.com> wrote in
      news:ec%db.4288 8$3r1.42045@new s02.bloor.is.ne t.cable.rogers. com:
      [color=blue]
      > I am having difficulty understanding this piece of code. I don't
      > understand why ++*ptr prints 7 and *ptr-- prints an unknown value. I
      > would have expected it to print 8. I would have to put brackets (*ptr)--
      > for it work. Thanx in advance.
      >
      > Joseph
      >
      > #include <stdio.h>
      >
      > int main(void)
      > {
      > int ctr =6;
      > int *ptr=&ctr;
      >
      > ++*ptr;
      > printf("ptr=%d\ n", *ptr); /*prints 7*/
      >
      > *ptr++;
      > printf("ptr=%d\ n", *ptr); /* prints ? */
      > }[/color]

      From:



      Since they associate right to left, with greedy operator binding we get:

      ++(*ptr)

      or

      "increment the dereferenced value of ptr" which is 7.

      Next case, same rule we get:

      *(ptr++)

      or

      "increment beyond the address of 'ctr' and then dereference it" which in
      this case would be junk.

      --
      - Mark ->
      --

      Comment

      • Mark A. Odell

        #4
        Re: operator precedance

        "Mark A. Odell" <nospam@embedde dfw.com> wrote in
        news:Xns94059AE 932CFECopyright MarkOdell@130.1 33.1.4:
        [color=blue]
        > From:
        >
        > http://freebsd.ntu.edu.tw/bsd/13/7/21.html[/color]

        Oops! Bad table, this is a C++ table but works the same for the operators
        we are discussing here.

        --
        - Mark ->
        --

        Comment

        • Martijn

          #5
          Re: operator precedance

          Joseph wrote:[color=blue][color=green]
          >> I am having difficulty understanding this piece of code. I don't
          >> understand why ++*ptr prints 7 and *ptr-- prints an unknown value. I
          >> would have expected it to print 8. I would have to put brackets
          >> (*ptr)-- for it work. Thanx in advance.
          >>
          >> Joseph
          >>[/color]
          >
          >
          >[color=green]
          >> #include <stdio.h>
          >>
          >> int main(void)
          >> {
          >> int ctr =6;
          >> int *ptr=&ctr;
          >>
          >> ++*ptr;
          >> printf("ptr=%d\ n", *ptr); /*prints 7*/
          >>
          >> *ptr++;
          >> printf("ptr=%d\ n", *ptr); /* prints ? */
          >> }[/color][/color]

          A slightly different table, containing mostly the same information, but also
          the direction of associativity.

          Operator Precedence and Associativity Rules in C / C++

          =============== =============== =============== =============== =============== =
          :: scope resolution (C++, e.g. name::member) left-to-right
          :: global (C++, e.g. ::name)
          ---------------------------------------------------------------------------
          -
          ( ) function call left-to-right
          [ ] array element
          . class, structure or union member
          -> pointer reference to member
          :: scope access / resolution (C++)
          sizeof size of object in bytes
          sizeof size of type in bytes
          ---------------------------------------------------------------------------
          -
          ++ post increment (lvalue++) right-to-left
          ++ pre increment (++lvalue)
          -- post decrement (lvalue--)
          -- pre decrement (--lvalue)
          ~ bitwise complement
          ! logical not
          - unary minus
          + unary plus
          & address of
          * contents of
          new create object (C++)
          delete destroy object (C++)
          delete[] destroy array (C++)
          (type) cast to type
          ---------------------------------------------------------------------------
          -
          .* member pointer (C++) left-to-right
          ->* pointer reference to member pointer (C++)
          ---------------------------------------------------------------------------
          -
          * multiply left-to-right
          / divide
          % remainder
          ---------------------------------------------------------------------------
          -
          + add left-to-right
          - subtract
          ---------------------------------------------------------------------------
          -
          << bitwise left shift left-to-right[color=blue][color=green]
          >> bitwise right shift[/color][/color]
          ---------------------------------------------------------------------------
          -
          < scalar less than left-to-right
          <= scalar less than or equal to[color=blue]
          > scalar greater than
          >= scalar greater than or equal to[/color]
          ---------------------------------------------------------------------------
          -
          == scalar equal left-to-right
          != scalar not equal
          ---------------------------------------------------------------------------
          -
          & bitwise and left-to-right
          ---------------------------------------------------------------------------
          -
          ^ bitwise exclusive or left-to-right
          ---------------------------------------------------------------------------
          -
          | bitwise or left-to-right
          ---------------------------------------------------------------------------
          -
          && logical and left-to-right
          ---------------------------------------------------------------------------
          -
          || logical inclusive or left-to-right
          ---------------------------------------------------------------------------
          -
          ? : conditional expression right-to-left
          ---------------------------------------------------------------------------
          -
          = assignment operator right-to-left
          also += -= *= /= %=
          &= ^= |= >>= <<=
          ---------------------------------------------------------------------------
          -
          , sequential expression left-to-right
          ---------------------------------------------------------------------------
          -

          All of the operators in this table can be overloaded (C++) except:

          . C++ direct component selector
          .* C++ dereference
          :: C++ scope access/resolution
          ?: Conditional

          --
          Martijn



          Comment

          • Mac

            #6
            Re: operator precedance

            On Mon, 29 Sep 2003 19:13:42 +0000, Mark A. Odell wrote:
            [color=blue]
            > Joseph <kermit24@roger s.com> wrote in
            > news:ec%db.4288 8$3r1.42045@new s02.bloor.is.ne t.cable.rogers. com:
            >[color=green]
            >> I am having difficulty understanding this piece of code. I don't
            >> understand why ++*ptr prints 7 and *ptr-- prints an unknown value. I
            >> would have expected it to print 8. I would have to put brackets (*ptr)--
            >> for it work. Thanx in advance.
            >>
            >> Joseph
            >>
            >> #include <stdio.h>
            >>
            >> int main(void)
            >> {
            >> int ctr =6;
            >> int *ptr=&ctr;
            >>
            >> ++*ptr;
            >> printf("ptr=%d\ n", *ptr); /*prints 7*/
            >>
            >> *ptr++;
            >> printf("ptr=%d\ n", *ptr); /* prints ? */
            >> }[/color]
            >
            > From:
            >
            > http://freebsd.ntu.edu.tw/bsd/13/7/21.html
            >
            > Since they associate right to left, with greedy operator binding we get:
            >
            > ++(*ptr)
            >
            > or
            >
            > "increment the dereferenced value of ptr" which is 7.
            >
            > Next case, same rule we get:
            >
            > *(ptr++)
            >
            > or
            >
            > "increment beyond the address of 'ctr' and then dereference it" which in
            > this case would be junk.[/color]

            Oh, no. that line isn't the problem. The value which is dereferenced in
            this line of code is the value ptr held before it was incremented. So, the
            value of *(ptr++) is well behaved, and is not by itself illegal, although
            it does leave ptr pointing beyond a valid object.

            The real problem comes later when ptr, which now points beyond a valid object,
            is dereferenced in the printf statement.

            Mac
            --

            Comment

            • Mark A. Odell

              #7
              Re: operator precedance

              "Mac" <foo@bar.net> wrote in news:pan.2003.0 9.30.03.14.42.7 74173@bar.net:
              [color=blue][color=green]
              >> Next case, same rule we get:
              >>
              >> *(ptr++)
              >>
              >> or
              >>
              >> "increment beyond the address of 'ctr' and then dereference it" which
              >> in this case would be junk.[/color]
              >
              > Oh, no. that line isn't the problem. The value which is dereferenced in
              > this line of code is the value ptr held before it was incremented. So,
              > the value of *(ptr++) is well behaved, and is not by itself illegal,
              > although it does leave ptr pointing beyond a valid object.
              >
              > The real problem comes later when ptr, which now points beyond a valid
              > object, is dereferenced in the printf statement.[/color]

              Ugh. What was I thinking? Thank you for the correction.

              --
              - Mark ->
              --

              Comment

              Working...