1. ## 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.

Thanks
Sara

2. 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.

3. 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.

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.