Home > front end >  question about modifying a pointer using another pointer in a function
question about modifying a pointer using another pointer in a function

Time:03-12

I have a question about the following code. I am not sure why the address stored in yAdd doesn't get updated to the same address stored in the variable x inside the addfun() function in the code below? what is wrong with my thinking?

#include <iostream>

using namespace std;

void fun(int *x) {
    *x = 55;
}

void addfun(int **x) {
    *x = new int[3];
    cout << x << endl;  // x stores the address of int[0]
                        // say it is 0x7ffc32e646a0
    cout << &x << endl;
}

int main()
{
    int y = 99;
    int *yAdd = &y;
    cout << y << endl;
    fun(yAdd);
    cout << y   << endl;
    cout << yAdd << endl; // original address stored in yAdd
    cout << "address of y is " << &y <<  endl;
    addfun(&yAdd);   // so I update the address stored in yAdd to be the same address
                    // as stored inside the x in the addfun function
    cout << yAdd << endl; // but how come when printing this, yAdd doesn't 
                    // show 0x7ffc32e646a0, but something else?
    return 0;
}

CodePudding user response:

addfun(&yAdd); 

This is passing the address of yAdd to this function, as a parameter.

yAdd is a pointer. The pointer is an address of some other object in your program. It doesn't matter what that object is, yAdd is its pointer, and whatever yAdd itself, its address gets passed.

cout << yAdd << endl; 

This shows the value of yAdd. Whatever this pointer is, its value is shown. And the address of this object, of yAdd, is something completely different.

void addfun(int **x)

x is a parameter to this function. x is a variable, that has nothing to do, whatsoever, with any other variable in your program. As such, it will have its own memory address, different than the memory address of everything else in your program (at least while it exists).

cout << &x << endl;

And this shows x's address. It is not showing the value of yAdd. It is not showing yAdds address, either. It is showing the address of x, a parameter to this function.

All of these are separate, distinct, objects and values in your program.

CodePudding user response:

Within the function addfun

void addfun(int **x) {
    *x = new int[3];
    cout << x << endl;  // x stores the address of int[0]
                        // say it is 0x7ffc32e646a0
    cout << &x << endl;
}

the statement

    cout << x << endl;  // x stores the address of int[0]
                        // say it is 0x7ffc32e646a0

prints the address of the original pointer yAdd passed to the function

addfun(&yAdd);

In main this statement

cout << yAdd << endl;

outputs the value assigned to the pointer within the function addfun in this statement

    *x = new int[3];

that is the address of the dynamically allocated memory.

The assigned value to the pointer yAdd and the address of the pointer itself are two different values.

CodePudding user response:

I changed the code to print out some more and label it better:

#include <iostream>

using namespace std;

void fun(int *x) {
    *x = 55;
}

void addfun(int **x) {
    cout << endl << "addfun()" << endl;
    cout << "x  = " <<  x << endl;
    cout << "*x = " << *x << endl;
    cout << "new int[3]" << endl;
    *x = new int[3];
    cout << "x  = " <<  x << endl;
    cout << "*x = " << *x << endl;
    cout << "addfun() done" << endl << endl;
}

int main()
{
    int y = 99;
    int *yAdd = &y;
    cout << y << endl;
    fun(yAdd);
    cout << y   << endl;
    cout << "&yAdd = " << &yAdd << endl;
    cout << "yAdd  = " << yAdd << endl;
    cout << "*yAdd = " << *yAdd << endl;
    cout << "address of y is " << &y <<  endl;
    addfun(&yAdd);   // so I update the address stored in yAdd to be the same address
                    // as stored inside the x in the addfun function
    cout << "&yAdd = " << &yAdd << endl;
    cout << "yAdd  = " << yAdd << endl;
    cout << "*yAdd = " << *yAdd << endl;
    return 0;
}

This produces for example the following output:

99
55
&yAdd = 0x7ffc9e47fe58
yAdd  = 0x7ffc9e47fe54
*yAdd = 55
address of y is 0x7ffc9e47fe54

addfun()
x  = 0x7ffc9e47fe58
*x = 0x7ffc9e47fe54
new int[3]
x  = 0x7ffc9e47fe58
*x = 0x564ab462d2c0
addfun() done

&yAdd = 0x7ffc9e47fe58
yAdd  = 0x564ab462d2c0
*yAdd = 0

As you can see when you enter addfun the value of x is the address of yAdd which points to y. Then you allocate the int[3] and assign the result to *x. That is you store the address of int[3] into where x points to, namely yAdd. The value of x is not changed.

After the call to addfun you can see that the address of yAdd is still the same as and the same as x was but where yAdd points to has now been changed to int[3].

I think the bit where you got confused is *x = .... That changes the things x points at and not x itself.

  • Related