Pointer math

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

    Pointer math

    Hi group!

    I have here five different declarations but I have some problems to
    understand this concept. I know there are more examples if
    I would use parentheses but I think the following ones are common.

    What I've learned..

    int myArray[3] = { 0, 1, 2 };
    int *ptr = myArray;

    1. *ptr++
    2. *++ptr
    3. ++*ptr
    4. ++*++ptr
    5. ++*ptr++

    1. printf("%d",*pt r++);

    result: 0 first array element

    because the postfix increment operator has a higher precedence
    than the dereferencing operator. Associativity LEFT to RIGHT. That
    would mean the same as *(ptr++) First
    incrementing the pointer then the OLDER value will be fetched.

    But why the older value? The incrementing process was the first
    action?

    2. printf("%d",*++ ptr);

    result: 1 second array element

    The dereferencing and the prefix operators have the same
    precedence. Associativity RIGHT To LEFT.That
    would mean *(++ptr) First incrementing the pointer then fetching
    the value.

    3. printf("%d",++* ptr)

    result: 1 second array element

    The dereferencing and the prefix operators have the same
    precedence. Associativity RIGHT to LEFT. The
    would mean++(*ptr). First fetching the value THEN incrementing.

    But why was the result 1 and not 0?

    4. printf("%d",++* ++ptr)

    result: 2 the third array element

    The dereferencing and the prefix operators have the same
    precedence. Associativity RIGHT to LEFT. That would mean first
    incrementing the pointer THEN fetching the value THEN incrementing
    that value?

    5. printf("%d",++* ptr++);

    result: 1 the second array element

    The postfix operator has a higher precedence than the prefix and
    dereferencing operator. That would mean
    the same as ++*(ptr++). First the pointer will be increment then
    fetching the value and then incrementing that
    value. Here the Associativity is tricky.

    I hope I'll get some clarity.

    Thanks for any help!


    Tom
  • maverik

    #2
    Re: Pointer math

    On Nov 11, 3:52 pm, tfelb <tomico...@gmai l.comwrote:
    Hi group!
    >
    I have here five different declarations but I have some problems to
    understand this concept. I know there are more examples if
    I would use parentheses but I think the following ones are common.
    >
    What I've learned..
    >
    int myArray[3] = { 0, 1, 2 };
    int *ptr = myArray;
    >
    1. *ptr++
    2. *++ptr
    3. ++*ptr
    4. ++*++ptr
    5. ++*ptr++
    >
    1. printf("%d",*pt r++);
    Ok, this is gets the value from ptr - it's 0, because after
    int *ptr = myArray;
    it points to the zeroth element. So printf prints 0, and the this
    (local!) value is incremented.
    >
    2. printf("%d",*++ ptr);
    That's clear. ptr points to the zeroth element. After increment it
    points to the first elemnt, so it's value is 1.
    3. printf("%d",++* ptr)
    That's easy too: ptr points to the zeroth element, you get it's value
    and increments in, so you get 0 + 1 == 1.
    4. printf("%d",++* ++ptr)
    After ++ptr ptr points to the first element of the array, you get its
    value and increments it, so you get 2.
    5. printf("%d",++* ptr++);
    Here you get value of the ptr that points to the zeroth element,
    increments it (prefix ++), the printf prints it (of course it's 1),
    then the local value increments again (postfix ++).


    Comment

    • Andrey Tarasevich

      #3
      Re: Pointer math

      tfelb wrote:
      >
      int myArray[3] = { 0, 1, 2 };
      int *ptr = myArray;
      >
      1. *ptr++
      2. *++ptr
      3. ++*ptr
      4. ++*++ptr
      5. ++*ptr++
      >
      1. printf("%d",*pt r++);
      >
      result: 0 first array element
      Yes.
      because the postfix increment operator has a higher precedence
      than the dereferencing operator.
      Yes.
      Associativity LEFT to RIGHT.
      Unary operators don't have any "associativity" .
      That
      would mean the same as *(ptr++)
      Yes.
      First incrementing the pointer then the OLDER value will be fetched.
      "First"? No. Neither operator precedence nor parentheses impose any
      temporal ordering on the operator execution process. No one knows what
      happens "first" or "next". The only thing that we know here is that
      'ptr++' evaluates to the original value of 'ptr', and that by the next
      sequence point the value of 'ptr' will get updated.
      But why the older value?
      Because that's what the specification of the postfix increment says.
      Postfix increment always returns the original ("older") value of its
      operand.
      The incrementing process was the first action?
      There's no such thing as "first action" here. Regardless of what happens
      and when, postfix increment always returns the original value - that's
      all you really need to know in this case.
      2. printf("%d",*++ ptr);
      >
      result: 1 second array element
      Yes.
      The dereferencing and the prefix operators have the same
      precedence.
      Er... No. There's no such thing as "precedence " for unary operators
      specified on the same side of the operand. Both '*' and prefix '++' are
      specified on the left. The one that "closer" to the operand ('ptr')
      applies to the operand, the one that's further from the operand applies
      to the result of the previous operator.
      Associativity RIGHT To LEFT.
      No. Again, there's no such thing as "associativ ity" with unary operators.
      That would mean *(++ptr)
      Yes. Again, simply because '++' is "closer" to 'ptr' than '*'.
      First incrementing the pointer then fetching
      the value.
      Wrong again. It means that '*' is applied to the incremented value of
      'ptr' (i.e. to 'ptr + 1'). When the value of 'ptr' will actually change
      - no one knows. It will change by the next sequence point, that's for
      sure. The compiler is free to evaluate it as follows

      1. read *(ptr + 1)
      2. change the value of 'ptr' to 'ptr + 1'

      As you can see, the actuall incrementing of the pointer is the last (not
      first) step in this case.
      3. printf("%d",++* ptr)
      >
      result: 1 second array element
      No. The result is the new value of the _first_ array element.
      The dereferencing and the prefix operators have the same
      precedence. Associativity RIGHT to LEFT.
      Both statement are wrong/irrelevant. See above.
      The
      would mean++(*ptr). First fetching the value THEN incrementing.
      Same mistakes as before. What it really means is that '++' is applied to
      the result of '*ptr'. Since this is the prefix '++', it is guaranteed
      that it evaluates to the new value of the operand.
      But why was the result 1 and not 0?
      Because the new value is 1.
      4. printf("%d",++* ++ptr)
      >
      result: 2 the third array element
      Again, no. The result is the new value of the _second_ array element.
      The dereferencing and the prefix operators have the same
      precedence. Associativity RIGHT to LEFT. That would mean first
      incrementing the pointer THEN fetching the value THEN incrementing
      that value?
      Same mistakes. It means: calculate the new value of 'ptr', dereference
      it, calculate the new value of that memory location; also update 'ptr'
      and that memory location before the next sequence point.
      5. printf("%d",++* ptr++);
      >
      result: 1 the second array element
      No. The result is the new value of the _first_ array element.

      All the same mistakes as above.

      --
      Best regards,
      Andrey Tarasevich

      Comment

      • Doug Miller

        #4
        Re: Pointer math

        In article <877i7anxi9.fsf @bsb.me.uk>, Ben Bacarisse <ben.usenet@bsb .me.ukwrote:
        >Another way to look at it, is simply that ++x has the value x + 1 with
        >the side effect of incrementing x by the next sequence point. x++ has
        >the value x with the same side effect.
        Very clearly and succinctly put. Thank you.

        Comment

        Working...