Thread: Program about pointers

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    132

    Program about pointers

    Hi, everyone. I was studying a C program that applies the ++ and * operatores on pointers, but I don't understand the output. The code is:

    Code:
    #include <stdio.h>
    int data[2] = {100, 200};
    int moredata[2] = {300, 400};
    
    int main(void)
    {
        int * p1, * p2, * p3; //declaration of three pointers
    
        p1 = p2 = data; //p1 = p2 = &data[0]
        p3 = moredata; //p3 = &moredata[0]
        printf("  *p1 = %d,   *p2 = %d,     *p3 = %d\n", *p1, *p2, *p3);
        printf("*p1++ = %d, *++p2 = %d, (*p3)++ = %d\n", *p1++, *++p2, (*p3)++);
        printf("  *p1 = %d,   *p2 = %d,     *p3 = %d\n", *p1, *p2, *p3);
    
        return 0;
    }
    The output is:

    Program about pointers-image2-jpg

    How come *p1++ is still 100? The way I see it, first p1 is incremented, poiting to data[1], and then the * operator should get 200 instead of 100. But this value is only obtained in the following printf line. And why is (*p3)++ 300 and not 301?

    Thanks in advance!

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Read this: C Operator Precedence Table. Pay particular attention to note #2 at the bottom. The postfix happens after the values are passed to printf.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    132
    Thank you! So, in

    Code:
    *p1++
    ,

    the * operator has higher precedence and even though I use the ++ on the same statement, its effect is only obtained after that statement?

    In the part

    Code:
    *++p2
    ,

    how come it already displays 200 without the delay?

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    132
    I think I am understanding it now. The point is that the postfix operator ++ has, in general, higher precedence than some operators bellow it on the table, except when there is the dereferencing operator in the statement - in that case, * goes first, right?

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by stdq View Post
    Code:
    *p1++
    ,

    the * operator has higher precedence and even though I use the ++ on the same statement, its effect is only obtained after that statement?
    No, reread the table in the link I provided. The ++ has higher precedence than the *. In this case, it's a post-fix increment, so the increment happens after. That means that all the other stuff is evaluated before the increment happens. p1 is dereferenced, giving you 100, then that value (the 100, not the address) is passed to printf, and lastly p1 is incremented to point to the next spot.

    Code:
    *++p2
    ,

    how come it already displays 200 without the delay?
    The ++ here also has higher precedence than the *, but is just lower than postfix ++. In this case, you're using a pre-fix increment, so the increment happens before the other stuff is evaluated. p2 is incremented to the second spot in the array, then the * dereferences it and passes 200 to printf.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Yes, the ++ operator has higher precedence. The difference is when the increment happens. If the ++ is on the left of something, it is incremented, and that new value used for the expression. If it's on the right, the old value is used, and the increment happens at the very end. It might help you to do some reading on sequence points: Sequence point - Wikipedia, the free encyclopedia and Question 3.8.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by stdq View Post
    How come *p1++ is still 100? The way I see it, first p1 is incremented, poiting to data[1], and then the * operator should get 200 instead of 100. But this value is only obtained in the following printf line. And why is (*p3)++ 300 and not 301?
    *p1++ will first retrieve the value and then increment the pointer... if you want it to increment first use *(++p1)

    (*p3)++ will be incremented AFTER retrieving the value ... If you want it to increment before retrieving the value use ++(*p3)

    And yes, it's enough to make you pull your hair out on a good day....

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    132
    Thank you, everyone!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with program using pointers !!!
    By yoursport in forum C++ Programming
    Replies: 6
    Last Post: 04-12-2005, 01:39 AM
  2. need some pointers on program
    By noob2c in forum C++ Programming
    Replies: 7
    Last Post: 12-14-2003, 05:16 AM
  3. Pointers program
    By Jamina in forum C++ Programming
    Replies: 3
    Last Post: 08-06-2003, 02:31 PM
  4. I need help with pointers in my program
    By CheeseMonkey in forum C++ Programming
    Replies: 3
    Last Post: 12-01-2001, 09:31 PM