Thread: what is the difference between them..

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    what is the difference between them..

    what is the difference between them..
    *(p++) and *(++p)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Whether there is a net difference depends on context. What do you think is the difference?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i think there is no difference
    in both cases it points to the next address

    i gives the value of the next cell

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    i think there is no difference
    Observe the output of this program and draw your conclusions:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int numbers[2] = {1, 2};
    
        int *p = numbers;
        printf("%d\n", *(p++));
        printf("%d\n", *p);
    
        p = numbers;
        printf("%d\n", *(++p));
        printf("%d\n", *p);
    
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    it against the logic of cols

    i used that the cols are separating
    so the part inside the cols are separated from the operation outside the cols.
    but here
    Code:
    printf("%d\n", *(p++));
    it returns the value inside p goes to printf and then return back to the cols and increases the address by 1.

    why???

    it should be like an onion
    do whats inside the cols the go outside
    like in if statement

    if ((a)&&(b)) etc..
    i evaluates "a" and "b" anf then compares them like onion

    ??

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    it returns the value inside p goes to printf and then return back to the cols and increases the address by 1.

    why???
    The result of p++ is p. Therefore, the result of *(p++) is *p. The increment is a side effect. Incidentally, *(p++) is equivalent to *p++.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    so *(p++) says
    first do the operation outside and then go back and increase p by 1
    correct?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    so *(p++) says
    first do the operation outside and then go back and increase p by 1
    correct?
    No, it says "first take the current value of p, then increment p" and then look at the address of the expression.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    "first take the current value of p, then increment p" and then look at the address of the expression."

    "first take the current value of p"

    means go with the first value to the printf

    then "increment p"
    p has increased address
    correct?

  10. #10
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    What *(p++) means, is this:

    Increment the position in memory that p points to, so, if it originally pointed to element 25 of an array, increment p to point to element 26. However, since the ++ is on the right side, increment it *after* accessing the memory. *(++p) is slightly different.

    Example program:

    Code:
    #include <stdio.h>
    
    int main() {
        int array[5] = {0, 2, 4, 6, 8};
        int *p = &array[1];
        printf("%i\n", *(p++));
        printf("%i\n", *p);
        return 0;
    }
    Example program output:

    Code:
    2
    4
    However, another example, this time for *(++p):

    Code:
    #include <stdio.h>
    
    int main() {
        int array[5] = {0, 2, 4, 6, 8};
        int *p = &array[1];
        printf("%i\n", *(++p));
        printf("%i\n", *p);
        return 0;
    }
    The output this time is

    Code:
    4
    4
    Last edited by Nightowl; 03-19-2009 at 01:31 PM.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding the difference in two times
    By muthus in forum C++ Programming
    Replies: 4
    Last Post: 01-24-2008, 06:36 PM
  2. Difference Equations / Recurrence Relations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-05-2007, 10:26 AM
  3. What's the difference between var++ and ++var
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 05-31-2007, 02:27 AM
  4. Replies: 10
    Last Post: 11-06-2005, 09:29 PM
  5. how to get difference of digits
    By Leeman_s in forum C++ Programming
    Replies: 5
    Last Post: 12-20-2001, 08:32 PM