The Wayback Machine - https://web.archive.org/web/20240715024139/https://www.geeksforgeeks.org/passing-by-pointer-vs-passing-by-reference-in-cpp/
Open In App

Passing By Pointer vs Passing By Reference in C++

Last Updated : 06 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// 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++




// 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. 

  • A pointer can be re-assigned while a reference cannot, and must be assigned at initialization only.
  • The pointer can be assigned NULL directly, whereas the reference cannot.
  • Pointers can iterate over an array, we can use increment/decrement operators to go to the next/previous item that a pointer is pointing to.
  • A pointer is a variable that holds a memory address. A reference has the same memory address as the item it references.
  • A pointer to a class/struct uses ‘->’ (arrow operator) to access its members whereas a reference uses a ‘.’ (dot operator)
  • A pointer needs to be dereferenced with * to access the memory location it points to, whereas a reference can be used directly.

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

C++




// 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++? 

  • References are usually preferred over pointers whenever we don’t need “reseating”.
  • If we want to use NULL in our function arguments, prefer pointers.
  • Overall, Use references when you can, and pointers when you have to.

 



Previous Article
Next Article

Similar Reads

C++ Pointer To Pointer (Double Pointer)
In C++ a Pointer is a variable that is used to store the memory address of other variables. It is a variable that points to a data type (like int or string) of the same type and is created with the * operator. Syntax of a Pointer in C++: data_type_of_pointer *name_of_variable = &amp; normal_variable; What is a Pointer to a Pointer or Double Pointer
4 min read
Difference between Dangling pointer and Void pointer
Dangling pointer: A pointer pointing to a memory location that has been deleted (or freed) is called a dangling pointer. There are three different ways where Pointer acts as a dangling pointer: By deallocating memoryFunction CallWhen the variable goes out of scope Void pointer: Void pointer is a specific pointer type – void * – a pointer that point
2 min read
Passing Map as Reference in C++ STL
Prerequisite: Maps in C++ STL Pass by reference Elements in the map are in form of pairs where the first is key and another value, denoting key-value pairs. Also, all the key values are unique means no two elements can have the same key value. Passing maps by value is a costly task, costly in terms of computation and memory resources. When we pass
3 min read
When Do We Pass Arguments by Reference or Pointer in C++?
In C++, we can pass arguments to a function as a value, reference (or pointer). Each method has its unique benefits and uses. In this article, we will discuss the application or cases where we should pass the arguments by reference or a pointer. But first, let’s quickly revisit the definitions of pointers and references. Pointers in C++ Pointers ar
4 min read
Passing a vector to constructor in C++
When class member is a vector object (not a reference).We can simply assign in constructor. C/C++ Code // Passing vector object to a constructor. #include &amp;lt;iostream&amp;gt; #include &amp;lt;vector&amp;gt; using namespace std; class MyClass { vector&amp;lt;int&amp;gt; vec; public: MyClass(vector&amp;lt;int&amp;gt; v) { vec = v; } void print()
2 min read
How to delete an element from the Set by passing its value in C++
Given a Set, the task is to remove the last element from this Set in C++.Examples: Input: set = [10 20 30 70 80 90 100 40 50 60], valueOfElementToBeDeleted = 100 Output: 10 20 30 40 50 60 70 80 90 Input: set = [1 2 3 4 5], valueOfElementToBeDeleted = 3 Output: 1 2 4 5 Sets are a type of associative container in which each element has to be unique b
6 min read
Equation of straight line passing through a given point which bisects it into two equal line segments
Given a straight line which passes through a given point (x0, y0) such that this point bisects the line segment in two equal line segments. The task is to find the equation of this straight line.Examples: Input: x0 = 4, y0 = 3 Output: 3x + 4y = 24Input: x0 = 7, y0 = 12 Output: 12x + 7y = 168 Approach: Let PQ be the line and AB be the line segment b
4 min read
Passing Pointers to Functions In C++
Prerequisites: Pointers in C++Functions in C++ Passing Pointers to functions means declaring the function parameter as a pointer, at the function calling passing the address of the variable and that address will be stored by a parameter that is declared as a pointer. To change the value of any variable in the function we have to pass the address of
4 min read
Parameter Passing Techniques in C++
In C++, data must be sent to functions when they are called in order to perform operations. This data is called parameters or arguments and there are various parameter passing methods available in C++, each with merits and demerits of its own. In this article, we will discuss various parameter-passing techniques in C++. Basic TerminologiesFunction
4 min read
Parameter Passing Techniques in C
In C, there are different ways in which parameter data can be passed into and out of methods and functions. Let us assume that a function B() is called from another function A(). In this case, A is called the "caller function" and B is called the "called function or callee function". Also, the arguments which A sends to B are called actual argument
5 min read
Practice Tags :
three90RightbarBannerImg