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. |
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:
Contact Us