PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, July 6, 2022

[FIXED] How to visualize the pointers in the code snippet?

 July 06, 2022     c, dereference, gcc, pass-by-reference, pointers     No comments   

Issue

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 

Solution

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.



Answered By - Vlad from Moscow
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

1,215,095

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing