Thread: Pointer Question

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    40

    Pointer Question

    Hi,
    I am having problem understand one of the questions in our midterm. It is related to pointers.
    Code:
    6. What is the output of the following program?
    int f(int *x, int y) {
    *x += 2; y += 1;
    return *x + y;
    }
    int g(int x, int *y) {
    *y = x++;
    return x + *y;
    }
    int main( ) {
    int x = 3, y = 4;
    printf("%d ",f(&x, y));
    printf("%d ",g(x, &y));
    printf("%d %d \n",x, y);
    return 0;
    }
    (A) 10 11 5 5 (B) 10 11 6 6 (C) 10 12 5 5 (D) 10 11 6 7
    The answer is (A) but I get confused where did 11 come from.
    Could someone please help me understand this.

    Thanks
    Sara

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Walk through each line one at a time. Pay attention to what each function returns.
    1 - Increment x by 2.
    2 - Return x + y + 1. (Remember, the change to this y is local only.)
    3 - Increment y by x.
    4 - Return y + locally incremented x.
    5 - Display x and y.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by sara.stanley
    Hi,
    I am having problem understand one of the questions in our midterm. It is related to pointers.
    Code:
    6. What is the output of the following program?
    int f(int *x, int y) {
    *x += 2; y += 1;
    return *x + y;
    }
    int g(int x, int *y) {
    *y = x++;
    return x + *y;
    }
    int main( ) {
    int x = 3, y = 4;
    printf("%d ",f(&x, y));
    printf("%d ",g(x, &y));
    printf("%d %d \n",x, y);
    return 0;
    }
    (A) 10 11 5 5 (B) 10 11 6 6 (C) 10 12 5 5 (D) 10 11 6 7
    The answer is (A) but I get confused where did 11 come from.
    Could someone please help me understand this.

    Thanks
    Sara
    The reason is because once function f() returns in the first printf, the x variable contains a new value, because it was modified in f(), x on return contains 5. When the second printf executes, it calls g() with two arguments which each contain the values 5 and 4 respecively. In g(), y is assigned the value of x (which is 5) and then x is incremented, now x equals 6. When it adds x and y in the return statement, it produces the value 11 and returns that.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    40
    thanks alot I understand now. I was confused about this statment *y=x++;

    basically what is doing is that it puts the value of x in y and x is updated.

    I was thinking of this like this:

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Or you could just compile it and run it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 10:54 AM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  4. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 02:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM