"*x + *x++" Address/Pointer "?"

This is a discussion on "*x + *x++" Address/Pointer "?" within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main(void) { int i = 5; printf("&#37;d", g(&i)); } int g (int *x) { return (*x ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    10

    Question "*x + *x++" Address/Pointer "?"

    Code:
    #include <stdio.h>
    
    int main(void)
    {
         int i = 5;
         printf("&#37;d", g(&i));
    }
    
    int g (int *x)
    {
         return (*x + *x++);
    }
    10 is printed out.

    But if you change the return statement to:

    Code:
    return (*x + *x+=1);
    11 is printed out.

    Can someone explain to me why "++" before or after *x doesn't work and why "+=1" does?

    I always thought "++" and "+=1" were the same thing when used after a variable.
    Last edited by Marth_01; 11-05-2008 at 02:18 AM. Reason: Comment

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,306
    *x++ increments x, not *x.

    However, I believe that undefined behaviour is involved in both cases. You are reading a variable twice between consecutive sequence points where the variable is modified. To avoid this, perhaps you should implement g as:
    Code:
    int g(int *x)
    {
        int t = (*x)++;
        return *x + t;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    ++*x does work just like *x += 1, and t = *x++

    ++a isn't the same as a++

    *x + *x++ means you add 1 to 5 and don't save, then dereference x to get 5 + 5 = 10

    *x + ++*x means you add 1 to 5 and save the change back to x, then dereference x to get 6 + 6 = 12

    *x + (*x += 1) is the same thing, 6 + 6 = 12

    Someone can explain that in technical terms.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > *x + *x++ means you add 1 to 5 and don't save, then dereference x to get 5 + 5 = 10
    No it doesn't. ++ has greater precedence than *.

    You dereference, then add 1 to the address of x. For example:

    Code:
    [zac@neux ~]$ gcc -std=c89 -pedantic -Wall eg.c -o eg
    [zac@neux ~]$ cat eg.c 
    #include <stdio.h>
    
    int main(void)
    {
       int array[2] = {5, 10};
       int * x = array;
    
       printf("&#37;d\n", *(x++));
       printf("%d\n", *x);
       return 0;
    }
    [zac@neux ~]$ ./eg 
    5
    10
    Last edited by zacs7; 11-05-2008 at 02:52 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,306
    Quote Originally Posted by Dae
    ++*x does work just like *x += 1, and t = *x++
    You made the same mistake as Marth_01. Where the expression stands alone, ++*x means the same thing as (*x)++ and *x += 1. It is not the same as *x++, which increments x, not *x.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    This is where a operator precedence table comes in handy . This can all be avoided with use of parenthesis though. *x++ is far easier to read as *(x++)

    I'm not sure that *x + *(x++) is undefined (in this case). Since the increment of x is delayed until the end of the statement, after *x has been dereferenced.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,306
    Quote Originally Posted by zacs7
    *x++ is far easier to read as *(x++)
    True. On the other hand, because of the famous while (*x++ = *y++) ; string copying code, the behaviour of *x++ should be well known to the average C programmer.

    Quote Originally Posted by zacs7
    I'm not sure that *x + *(x++) is undefined. Since the increment of x is delayed until the end of the statement, after *x has been dereferenced.
    Consider C99 section 6.5 paragraph 2:
    "Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored." (Emphasis mine.)

    The first use of x does not determine the value to be stored. It determines the pointer to be dereferenced to be used in a different calculation. Consequently, there is undefined behaviour. C99 provides a concrete example when it states that that paragraph renders undefined this expression:
    Code:
    a[i++] = i;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by laserlight View Post
    You made the same mistake as Marth_01. Where the expression stands alone, ++*x means the same thing as (*x)++ and *x += 1. It is not the same as *x++, which increments x, not *x.
    Oh right, I wasn't thinking. That's obvious precedence. I need to use pointers more and be quiet.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Interesting, thanks for the research laserlight. I suppose the concrete example is pretty much what's going on here.

    Is then:
    Code:
    int x = 5;
    int d = x++ + x++;
    Defined? Since the sequence points do not matter (As far as I can tell).
    Last edited by zacs7; 11-05-2008 at 04:21 AM.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,306
    No, since the stored value of x is modified twice within consecutive sequence points (plus it has the same problem as x + x++ or x++ + x). Consider also that the resulting value of x could reasonably be 6 or 7, depending on your point of view.
    Last edited by laserlight; 11-05-2008 at 04:24 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I see. 'stored' was the word I was overlooking, thanks :-).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Use of "?" ":"
    By MK27 in forum C Programming
    Replies: 6
    Last Post: 12-03-2008, 03:21 AM
  2. What the heck is "?" ???
    By B0bDole in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 06-10-2005, 10:58 AM
  3. "?" and ":"
    By EvBladeRunnervE in forum C Programming
    Replies: 5
    Last Post: 02-04-2003, 12:13 AM
  4. "->" and "?" operators
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 01-20-2002, 08:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21