Updated March 18, 2023
Difference Between C++ Reference and Pointer
There is a significant difference between C++ reference vs pointer. A reference in C++ is an alternate name for an already existing variable. Once a reference variable is initialized, it can be used to refer to the same variable by another name. On the other hand, a pointer in C++ is a variable that stores the memory address of another variable. Like any variable, they are declared first, and then any variable’s address can be stored in them.
C++ Reference vs Pointer: Head-to-Head Comparison (Infographics)
Below are the top 7 differences between C++ Reference vs Pointer:
C++ Reference vs Pointer: Key Differences
Both are popular choices in the market; let us discuss some of the major differences:
- The primary difference between C++ Reference vs Pointer is that the former is referring to another variable while the latter is storing the address of a variable.
- References do not change an original variable, while if the pointer changes, it affects the original variable.
- A reference must initialize on the declaration, while it is not necessary to initialize a pointer after its declaration.
- One can create an array of pointers but not an array of references.
- One cannot assign a null value to a reference but can assign it to a pointer.
C++ Reference vs Pointer Comparison Table
The primary comparisons are as follows:
The basis of comparison | C++ Reference | Pointer |
Variables | A reference is simply an alias for an existing variable. The primary use of a reference variable is to serve as a pass-by-reference parameter in a function. When a reference variable passes to a function, the function operates on the original variable rather than a copy, which would be the case in pass-by-value. Any changes to the original variable inside the function will reflect outside of it as well. | Pointer variables store memory addresses rather than values of a specific data type such as int, double or char. These variables simplify programming by providing a way to reference the location of a particular variable in memory. |
Declaration | Declaration of a reference variable is done by adding an ampersand (&) symbol before an existing variable’s name. When used with an expression, the ampersand symbol denotes the “address of” operator. A reference variable provides an alternative name or alias for an existing variable.
Syntax:
// or
// or
For eg:
Now change the value of the Name
This signifies that a reference variable’s values can change in both the original and copy of the variable. |
One must declare the pointer variable before using it in the program. To declare a pointer variable, one prefixes the variable name with an asterisk “*” symbol to indicate that it is a pointer. The pointer variable is accompanied by the data type of the variable it is intended to point to, such as int or double.
Syntax:
// or
// or
For e.g.:
This pointer will hold the address. That address holds an int value.
The “*” indicates that a pointer is being declared and does not act as an operator. |
Reassignment | One cannot reassign a reference variable.
Example:
|
A pointer can be reassigned, and this feature comes in handy when a developer is implementing data structures like linked lists, trees, etc.
Example:
|
Memory Address | A reference variable shares the same memory address as the original variable. This means that a reference can pass to different functions and be stored in different classes. It always refers to the original variable until that variable goes out of scope or one deletes it. | A pointer has its own memory address and stores it on the stack. A pointer is an independent variable and can have new values assigned to itself. |
Null Value | A reference cannot have a null value assigned. | A pointer can have a null value assigned directly. |
Arguments | A reference variable can be referenced bypass by value. Here arguments are passed by value to the functions. A clone is made and sent to the function using it. Changes made to the copy have no effect on the original variable. | When we wish to change the original copy, then it can be done by passing a pointer of the object into the function. This is known as a pass-by reference. |
When to use | References are indirectly accessing a variable. Consider the following:
Example:
In short, it has usage in function parameters and reference types. |
Using pointers is without any pre-declaration.
One can use it to implement data structures and pointer arithmetic operations. |
Conclusion
Both C++ Reference and Pointer have their own uses. Although both are difficult to work on, they improve the efficiency of the program to a great extent. One can use pointers to implement data structures and algorithms and utilize references to use functions and parameters with return types.
Recommended Article
Here are some further comparison articles for improving understanding: