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.
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.
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.
Pass by Pointer
Pass by Reference
|We pass the address of arguments in the function call.||We pass the arguments in the function call.|
|The value of the arguments is accessed via the dereferencing operator *||The reference name can be used to implicitly reference a value.|
|Passed parameters can be moved/reassigned to a different memory location.||Parameters can’t be moved/reassigned to another memory address.|
|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.
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.
This article is contributed by Rohit Kasle. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
Please Login to comment...