Pass by Reference vs. Pass by Pointers?

What is the difference between the following two codes?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

void passReference (int &x);

int main ()
{
    int value = 10;
    passReference(value);

    cout << value << endl;

return 0;
}

void passReference (int &x){

    x = 20;
}




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

void passPointer(int *x);

int main ()
{
    int value = 10;
    passPointer(&value);

    cout << value << endl;

return 0;
}

void passPointer (int *x){

    *x = 20;
}




I realize they both do the same thing: change value to 20. But they do it in different ways. Is one way superior to another, or more efficient? What exactly is happening in the above two codes, particularly the second one?

Thank you for any help.
Thanks, just read the link.

However, I still dont understand how "Pass-by-Pointer" works. How does it change the value of the variable in main?

Can someone explain in simpler terms?
There are no simpler terms.

Do you know what a pointer is and how to use it?
When you pass a pointer, you do so by value. The pointer holds a value which is an address. Dereferencing the pointer (see the application of * on line 18 above) yields a reference to the object which is at that address.

When you pass an object by pointer, you pass a pointer containing the object's address by value.
When you pass a pointer, you do so by value. The pointer holds a value which is an address. Dereferencing the pointer (see the application of * on line 18 above) yields a reference to the object which is at that address.

When you pass an object by pointer, you pass a pointer containing the object's address by value.


Ok, but if you are actually passing by value when using a pointer, how does that change the value of the variable in main? I thought passing by value does NOT modify the original variable.

Thats what Im confused about...
Last edited on
Ok, but if you are actually passing by value when using a pointer, how does that change the value of the variable in main? I though passing by value does NOT modify the original variable.


You're passing the pointer by value. No pointer value is changed in main. Dereferencing the pointer allows you to access the object at the address contained in the pointer.

On line 9 in the pass-by-pointer snippet above, a temporary (unnamed) variable is generated which is the pointer passed to the function. If you were to do x = 20; within the body of the function it would not change the address of the variable who's address was fed to the function.

Perhaps using a named variable rather than the temporary will shed some light:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// http://ideone.com/csJ2Ad
#include <iostream>
using namespace std;

void passPointer(int *x);

int main()
{
    int value = 10;
    int* vPtr = &value;

    std::cout << "value before function call: " << value << '\n';
    std::cout << "Address contained by vPtr before function call: " << vPtr << '\n';

    passPointer(vPtr);

    std::cout << "value after function call: " << value << '\n';
    std::cout << "Address contained by vPtr after function call: " << vPtr << '\n';
}

void passPointer(int *x){
    int local_variable;

    std::cout << "Address of local_variable: " << &local_variable << '\n';

    *x = 20;                    // derefence allows one to get a reference to the object pointed to.
    x = &local_variable;        // not dereferencing means we're modifying a copy of the pointer.
}
... what is actually passed.

"by value" means making a copy of a value. "pass a pointer" means that the value that is copied is an address.

Dereferencing an address gives access to whatever is at that location. The following code does copy an address and then dereference it:
1
2
3
4
5
6
7
8
9
10
int main ()
{
  int value = 10;
  int * px = &value;
  int * py = px;
  int * pz = py;
  *pz = 20;
  cout << value << endl;
  return 0;
}
Simple, you are not copying the int.
x, the pointer (type int*) is the memory location of the value.
*x gets you a reference (type int&) to this value and -as in the pass by ref example- will edit the original value.

With pointers you can choose to point to another value (x = &another_int;).
This cannot be done with references, once a reference is initialized, it cannot refer to a different variable.
Also, when used as a parameter, you can specify a NULL pointer to some functions, but you cannot specify a NULL reference (well, you could, but it's not gonna work)
Last edited on
I think I understand now.

Basically, when the function call is made, the address of "value" is passed to the passPointer function. This function then dereferences x (value of x being 10), and changes it to 20. Since the address of 'value' is the same as the address of 'x', when the 'x' is changed to 20, so is 'value' in main. So its all because the address of both is the same, so when the value being pointed to changes for one, it changes for the other also.

Am I understanding this correctly?
Am I understanding this correctly?

No.


Basically, when the function call is made, the address of "value" is passed to the passPointer function.

Yes, as a pointer. Within the function the pointer x holds that address.


This function then dereferences x (value of x being 10), and changes it to 20.

No. x is a pointer. The value of x is the address of a variable of type int. We dereference x in order to obtain a reference to the variable whose address x holds.


Since the address of 'value' is the same as the address of 'x', when the 'x' is changed to 20, so is 'value' in main.
x and value have different addresses. The value held by x is the address of the variable value in main.


So its all because the address of both is the same, so when the value being pointed to changes for one, it changes for the other also.

No. It isn't possible for two different variables to reside at the same address coincidentally (barring union trickery.)

The address of a pointer and the address contained by a pointer are distinctly different things.
No. x is a pointer. The value of x is the address of a variable of type int. We dereference x in order to obtain a reference to the variable whose address x holds.


Ok, so the reference to the variable whose address x holds is 10 (i.e. the value pointed ot by x is 10). However, its then changed to 20. Correct?

But then how does 'value' back in main change to 20? Did simply dereferencing the address of 'value' in main to 20 also change 'value' in main?

No. It isn't possible for two different variables to reside at the same address coincidentally (barring union trickery.)


Maybe I said it wrong, but thats not what I meant. I meant to say that the parameter *x is holding the same value as the address of 'value'. Thus, when its dereferenced to 20, 'value' back in main is also changed to 20. This is what my understanding is.

I hope im being clear here...
I hope im being clear here...

I think you've got the gist. ;)
Topic archived. No new replies allowed.