Open In App

Passing By Pointer vs Passing By Reference in C++

In C++, we can pass parameters to a function either by pointers or by reference. In both cases, we get the same result. So, what is the difference between Passing by Pointer and Passing by Reference in C++?

Let’s first understand what Passing by Pointer and Passing by Reference in C++ mean:



Passing by Pointer

Here, the memory location (address) of the variables is passed to the parameters in the function, and then the operations are performed. It is also called the call by pointer method.




// C++ program to swap two numbers using
// pass by pointer
#include <iostream>
using namespace std;
 
void swap(int *x, int *y)
{
    int z = *x;
    *x = *y;
    *y = z;
}
 
// Driver Code
int main()
{
    int a = 45, b = 35;
    cout << "Before Swap\n";
    cout << "a = " << a << " b = " << b << "\n";
 
    swap(&a, &b);
 
    cout << "After Swap with pass by pointer\n";
    cout << "a = " << a << " b = " << b << "\n";
}

Output

Before Swap
a = 45 b = 35
After Swap with pass by pointer
a = 35 b = 45

Passing By Reference

It allows a function to modify a variable without having to create a copy of it. We have to declare reference variables. The memory location of the passed variable and parameter is the same and therefore, any change to the parameter reflects in the variable as well.

It is also called the Call by Reference method.




// C++ program to swap two numbers using
// pass by reference
#include <iostream>
using namespace std;
void swap(int& x, int& y)
{
    int z = x;
    x = y;
    y = z;
}
 
int main()
{
    int a = 45, b = 35;
    cout << "Before Swap\n";
    cout << "a = " << a << " b = " << b << "\n";
 
    swap(a, b);
 
    cout << "After Swap with pass by reference\n";
    cout << "a = " << a << " b = " << b << "\n";
}

Output
Before Swap
a = 45 b = 35
After Swap with pass by reference
a = 35 b = 45

Pass by Pointer vs Pass by Reference

The following table lists the major differences between the pass-by-pointer and pass-by-reference methods.

Parameters

Pass by Pointer

Pass by Reference

Passing Arguments

We pass the address of arguments in the function call. We pass the arguments in the function call.

Accessing Values

The value of the arguments is accessed via the dereferencing operator * The reference name can be used to implicitly reference a value.

Reassignment

Passed parameters can be moved/reassigned to a different memory location. Parameters can’t be moved/reassigned to another memory address.

Allowed Values

Pointers can contain a NULL value, so a passed argument may point to a NULL or even a garbage value. References cannot contain a NULL value, so it is guaranteed to have some value.

Difference Between Reference Variable and Pointer Variable

A reference is the same object, just with a different name and a reference must refer to an object. Since references can’t be NULL, they are safer to use. 

Example: The following C++ program demonstrates the differences.




// C++ program to demonstrate differences
// between pointer and reference
#include <iostream>
using namespace std;
 
struct demo {
    int a;
};
 
int main()
{
    int x = 5;
    int y = 6;
    demo d;
 
    int* p;
    p = &x;
    p = &y; // 1. Pointer reinitialization allowed
 
    int& r = x;
    // &r = y;                 // 1. Compile Error
 
    r = y; // 1. x value becomes 6
 
    p = NULL;
    // &r = NULL;             // 2. Compile Error
 
    // 3. Points to next memory location
    p++;
 
    // 3. x values becomes 7
    r++;
 
    cout << &p << " " << &x << '\n'; // 4. Different address
    cout << &r << " " << &x << '\n'; // 4. Same address
 
    demo* q = &d;
    demo& qq = d;
 
    q->a = 8;
    // q.a = 8;                 // 5. Compile Error
    qq.a = 8;
    // qq->a = 8;             // 5. Compile Error
 
    // 6. Prints the address
    cout << p << '\n';
 
    // 6. Print the value of x
    cout << r << '\n';
 
    return 0;
}

Output
0x7ffdfc7bead8 0x7ffdfc7bead4
0x7ffdfc7bead4 0x7ffdfc7bead4
0x4
7

 Which is preferred, Passing by Pointer Vs Passing by Reference in C++? 

 


Article Tags :
C++