Difference Between C++ Reference and Pointer
Though C++ reference to pointer seems to be similar, there is a significant difference between both C++ Reference vs Pointer. A reference variable can be said as another name for an existing variable. Once this variable is initialized, the variable name can be used to refer to another variable. Pointers, on the other hand, are variables that store the address of a variable. Like any variable, these are declared first, and then any variable’s address can be stored in them.
Head To Head Comparison Between C++ Reference and Pointer (Infographics)
Below is the top 7 difference between C++ Reference vs Pointer
Key differences Between C++ Reference and Pointer
Both are popular choices in the market; let us discuss some of the major difference:
The main difference between C++ Reference vs Pointer is that one 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 is changed, it affects the original variable. A reference must be initialized on the declaration, while it is not necessary to initialize a pointer once it is declared. An array of pointers can be created, while an array of references cannot be created. A null value cannot be assigned to a reference, but it can be assigned to a pointer.
C++ Reference vs Pointer Comparison Table
The primary comparison is discussed below:
|The basis of comparison||C++ Reference||Pointer|
|Variables||A reference can be said as an alias to an existing variable. The main use of this variable is working as a parameter that works as a pass-by-reference. This is passed to a function. The function to which this is passed works on the original variable instead of the copy in a pass by value. The changes made inside the function will also be reflected outside.||Pointer variables are variables that store address. Unlike usual variables that we have like int, double and char it stores memory addresses. To make programming easier, the need for this variable arises. It is a variable that can store a value of a particular type.|
|Declaration||Declaration of a C++ reference variable is done by adding a ‘&’ symbol before a variable. When it is used with an expression it denotes that it is working as an address of the operator. It is used to provide another name to an existing variable.
Now change the value of Name
This signifies that a reference variable’s values can be changed in the original and copy of the variable.
|Before using a pointer anywhere in the program, it should be declared in advance. To declare a pointer and let the system know that it is a pointer, a variable is prefixed by a ‘*’. A pointer is accompanied by the data type, which can be an int or a double.
The * indicates that a pointer is being declared and does not act as an operator.
|Reassignment||A reference variable cannot be reassigned.
|A pointer can be reassigned, and this property comes in handy when a developer is implementing data structures like linked lists, trees, etc.
|Memory Address||A reference variable shares the same address as the original variable. These references can be passed to different functions; they can be stored in different classes, etc. A reference never points to a new variable until the old one is deleted or goes out of scope.||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 pass by reference.|
|When to use||References are indirectly accessing a variable. Consider the following
In short, this can be used in function parameters and reference types.
|Using pointers is without any pre-declaration.
This can be used to implement data structures and pointer arithmetic operations.
C++ Reference vs Pointer is difficult to work upon, but they improve the efficiency of the program to a great extent. They both C++ Reference vs Pointer have their own uses and serve their own purposes. You can use pointers when you wish to implement data structures and algorithms, while references when you wish to use functions and parameters with return types.
This has been a guide to the top differences between C++ Reference vs Pointer. Here we also discuss the key differences with infographics and comparison table. You may also have a look at the following articles to learn more –