1

I am finding it difficult to visualize this piece of code. I cannot seem to find the ans to this.

I did get the ans for

printf("**r = %d\n",**r);
printf("**s = %d\n",**s);

but other variables are not matching the ans. Please help.

int f(int ** r, int ** s) {
    int temp = ** r;
    int temp2 = **s;
    int * z = *r;
    *r = *s;
    *s = z;

    printf("**r = %d\n",**r);
    printf("**s = %d\n",**s);
    *z += 3;
    **s -= 8;
    **r -= 19;

    return temp + temp2;
}

int main(void) {
    int a = 80;
    int b = 12;
    int * p = &a;
    int * q = &b;
    int x = f(&p, &q);

    printf("x = %d\n", x);
    printf("*p = %d\n", *p);
    printf("*q = %d\n", *q);
    printf("a = %d\n", a);
    printf("b = %d\n", b);

    return EXIT_SUCCESS;
}

Expected output:

**r = 12
**s=80
x=92
*p=-7
*q=75
a=75
b=-7 
5
  • 3
    What output do you expect, and what do you get, instead? Commented Jun 6, 2020 at 16:33
  • **r = 12 **s=80 x=92 *p=-7 *q=75 a=75 b=-7 is what you should be getting but I am mixing up a b *p and *q
    – user13695064
    Commented Jun 6, 2020 at 16:44
  • I don't get you. Are you saying that this program is wrong? I am not sure as I was supposed to dry run this one but as it's was a ques from a online grader, I would suppose it works
    – user13695064
    Commented Jun 6, 2020 at 16:49
  • I edited your question with the expected result from your comment. Is it correct? (if not, edit the question again adding both the expected result and the actual one) Because it is exactly what I get (and what is expected after analyzing your code). So, what's the problem? You don't understand why this is the returned result? Commented Jun 6, 2020 at 17:05
  • Yeah, I didn't get the result
    – user13695064
    Commented Jun 7, 2020 at 4:41

1 Answer 1

1

In this declaration the pointers p and q are passed by reference to the function f.

int x = f(&p, &q);

The pointer p points to the variable a and the pointer q points to the variable b.

int a = 80;
int b = 12;
int * p = &a;
int * q = &b;

Within the function f declared like

int f(int ** r, int ** s);

the pointer r points to the pointer p and the pointer s points to the pointer q.

Correspondingly in these declarations

int temp = ** r;
int temp2 = **s;

the variable temp is initialized by the value of variable a and the variable temp2 is initialized by the value of the variable b.

You van imaging these declaration like

int temp = a;
int temp2 = b;

In this code snippet

int * z = *r;
*r = *s;
*s = z;

there are in fact swapped the the pointers p and q pointed to by the pointers r and s. That is after this code snippet the pointer r now points to the pointer q and the pointer s points to the pointer p.

You can imaging this like

*r = q;
*s = p;

The intermediate variable z

int * z = *r;

gets the value of the pointer p.

You can imaging this like

int * z = p;

This statement

*s = z;

did not change the value pointed to by s because before this statement the variable s already pointed to p due to preceding swapping the pointed values of the pointer r and the pointer s.

So these calls of printf

printf("**r = %d\n",**r);
printf("**s = %d\n",**s);

correspondingly will output the value of b and the value of a.

That is the output will be

**r = 12
**s = 80

As the pointer z has the value of the pointer p then after this statement

*z += 3;

the variable a will be increased by 3 and becomes equal to 83.

In these statements

**s -= 8;
**r -= 19;

the variable a will be decrease by 8 and becomes equal to 75 And the variable b is decreased by 19 and becomes equal to -7.

At last the function returns the sum of the initial values of the variables a and b

return temp + temp2;

that is 92.

In these statements in main

printf("x = %d\n", x);
printf("*p = %d\n", *p);
printf("*q = %d\n", *q);
printf("a = %d\n", a);
printf("b = %d\n", b);

there is outputted the value returned by the function f

printf("x = %d\n", x);

that is 92.

As the pointers p and q were swapped in the function then now the pointer p points to b and the pointer q points to a.

printf("*p = %d\n", *p);
printf("*q = %d\n", *q);

So these statements output

*p = -7
*q = 75

And these statements

printf("a = %d\n", a);
printf("b = %d\n", b);

outputs the new values of a and b themselves that is

a = 75
b = -7

As for this statements

printf("**r = %d\n",**r);
printf("**s = %d\n",**s);

then for example the expression *r points to the pointer p. So dereferencing the pointer p like *p that is the same as *( *r ) you will get the lvalue of the variable a.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.