2

I'm a bit confused on the following two cases. Both functions are passing by pointer. One leads to a change in the main, the other does not. I thought passing by pointer really shouldn't have any effect after the function is called since it produce a local copy of the pointer in the function. Any hints are appreciated

#include <vector>
#include <iostream>
using namespace std;
//
class A
{
        public:
                int b;
                A() {;}
};

//
void  test1(A *a)
{
    A t;
    t.b = 200;
    a = &t;
}

//
void  test2(A *a)
{
    a->b = 200;
}

//
int main()
{
    A a;
    a.b = 10;

    test1(&a);
    cout<<"a.b value is NOT changed"<<endl;
    cout<<a.b<<endl;

    test2(&a);
    cout<<"a.b value is changed"<<endl;
    cout<<a.b<<endl;
}


//.. the output is: 
//a.b value is NOT changed
//10
//a.b value is changed
//200
William
  • 75
  • 7
  • 3
    There's no such thing as "passing by pointer" in C++. There's either passing by value or passing by reference. You're passing everything by value. "a = &t;" set the parameter to `test1`'s function to some value. Which means nothing to everyone else, except `test1`. – Sam Varshavchik Aug 20 '19 at 15:30
  • 1
    "I thought passing by pointer really shouldn't have any effect after the function is called since it produce a local copy of the pointer in the function" - You can copy a pointer as much as you like. That doesn't change what it points *to* and you can change what it points to through *any* copy. The pointer itself is not important, what it *points to* is what is being charged. – Jesper Juhl Aug 20 '19 at 15:31
  • Try printing the objects addresses at each point, before calling test1, test2 and inside each function for a and address of b. You will get your answer. – ATul Singh Aug 20 '19 at 15:31
  • Partial Dupe: https://stackoverflow.com/questions/11842416/function-does-not-change-passed-pointer-c' – NathanOliver Aug 20 '19 at 15:31
  • 6
    `a` is a copy of the pointer given to `test1` or `test2` so changing what it points to doesn't do anything to the original pointer. In addition, if you had succeeded in doing what it looks like you are trying to do with `test1` the pointer would point to an object that no longer exists. Finally, the address of an object cannot change ever in c++. It's not possible to assign a new address to `&a`, which is what you seem to be trying to do. – François Andrieux Aug 20 '19 at 15:32
  • If you want to make changes to the object `a` points at (instead of changing which object it points at locally), change `a = &t;` to `*a = t;` That is, dereference `a` and assign a new value to the object. This could also be useful as a comparison: `void test3(A& a) { A t; t.b = 200; a = t; }` – Ted Lyngmo Aug 20 '19 at 15:48

1 Answers1

6

I though passing by pointer really shouldn't have any effect after the function is called since it produce a local copy of the pointer in the function.

Yes, the pointer parameter itself is passed by value, any modification on the pointer parameter itself has nothing to do with the argument. That's why the value doesn't change for the 1st code sample.

But it's different for the 2nd case; you're modifying the object pointed by the pointer. Both the parameter and argument pointers point to the same object, which gets changed.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405