In C++, reference and pointer seem to be similar, but some differences exist between them. A reference is a variable that is another name of the existing variable, while the pointer is a variable that stores the address of another variable.
In this article, we will discuss the difference between reference and pointer in C++. Before discussing their differences, we must know about references and pointers in C++.
What is a Reference in C++?
A reference is defined using the ampersand (&) symbol. An alias for an existing variable in C++ is a reference variable, which gives access to the same memory location with a different name. When we once initialized a reference to a variable, it cannot be modified to refer to another variable.
C++ Reference Example
Let us take an example to illustrate the reference in C++.
Example
#include <iostream>
using namespace std; //using standard namespace
int main() //main function
{
int i=8; // variable initialization
int &a=i; // creating a reference variable
cout<<"The value of 'i' variable is :"<<a;
return 0;
}
Output:
The value of 'i' variable is :8
Explanation:
In this example, we have created a reference variable, i.e., 'a' for 'i' variable. After creating a reference variable, we can access the value of 'i' with the help of 'a' variable.
What is a Pointer in C++?
A pointer is a variable that contains the address of another variable. It can be dereferenced with the help of (*) operator to access the memory location to which the pointer points. It can be utilized with any data type , such as arrays, basic data types, and user-defined types, including classes and structures.
C++ Pointer Example
Let us take an example to illustrate the pointer in C++.
Example
#include <iostream>
using namespace std; //using standard namespace
int main() { //main function
int x = 13, y = 9;
int addition;
int *ptr1 = &x;
int *ptr2 = &y;
addition = *ptr1 + *ptr2;
cout << "First number: " << *ptr1 << endl;
cout << "Second number: " << *ptr2 << endl;
cout << "Addition: " << addition << endl;
return 0;
}
Output:
First number: 13
Second number: 9
Addition: 22
Explanation:
In this example, we store the addresses of two integers x and y in pointers ptr1 and ptr2 and then access their values using dereferencing (ptr1 and ptr2) to calculate their sum. Finally, it prints the original values and the result of the addition.
Key differences between Reference and Pointer
There are several key differences between reference and pointer in C++. Some main differences are as follows:
Definition
In C++, a reference variable is another name for an already existing variable. It is mainly used in 'pass by reference' where the reference variable is passed as a parameter to the function, and the function to which this variable is passed works on the original copy of the variable. On the other hand, a Pointer is a variable that stores the address of another variable. It makes the programming easier as it holds the memory address of some variable.
Declaration
In C++, a reference variable can be declared by adding a '&' symbol before a variable. If this symbol is used in the expression, it will be treated as an address operator. On the other hand, before using a pointer variable, we should declare a pointer variable, and this variable is created by adding a '*' operator before a variable.
Reassignment
In C++, we cannot reassign the reference variable. On the other hand, the pointers can be re-assigned. It is useful when we are working with the data structures, such as linked lists, trees, etc.
Memory Address
In the case of reference, both the reference and actual variable refer to the same address. A reference always refers to the same variable it was initialized with. It cannot be changed to refer to another variable.
Reference Example:
Example
#include <iostream>
using namespace std; //using standard namespace
void func(int &);
int main() //main function
{
int i;
int &a=i;
cout << "The address of 'a' variable is : " <<&a<< endl;
cout << "The address of 'i' variable is : " <<&i<< endl;
return 0;
}
Output:
The address of 'a' variable is : 0x7fff078e7e44
The address of 'i' variable is : 0x7fff078e7e4
Explanation:
The above output shows that both the reference variable and the actual variable have the same address.
In the case of pointers , both the pointer variable and the actual variable will have different memory addresses.
Pointer Example
Example
#include <iostream>
using namespace std; //using standard namespace
int main() //main function
{
int k;
int *p;
p=&k;
cout<<"The memory address of p variable is :"<<&p;
cout<<"\nThe memory address of k variable is :"<<&k;
return 0;
}
Output:
The memory address of p variable is :0x7ffef9ea3db0
The memory address of k variable is :0x7ffef9ea3dac
NULL Value
In C++, we cannot assign the NULL value to the reference variable. On the other hand, the pointer variable can be assigned with a NULL value.
Indirection
In C++, pointers can have a pointer to pointer, which offers more than one level of indirection.
Pointer Example:
Example
#include <iostream>
using namespace std; //using standard namespace
int main() //main function
{
int *p;
int a=8;
int **q;
p=&a;
q=&p;
cout << "The value of q is : " <<*q<<endl;
return 0;
}
Output:
The value of q is : 0x7fffc17a95f4
Explanation:
In this example, the pointer 'p' is pointing to variable 'a' while 'q' is a double pointer that is pointing to 'p'. Therefore, we can say that the value of 'p' would be the address of the 'a' variable, and the value of the 'q' variable would be the address of the 'p' variable.
On the other hand, references only contain one level of indirection in C++.
Reference Example:
Example
#include <iostream>
using namespace std; //using standard namespace
int main() //main function
{
int a=8; // variable initialization
int &p=a; // creating a reference variable for ?a? variable.
int &&q=(p); // Only valid with std::move
return 0;
}
Output:
main.cpp: In function 'int main()':
main.cpp:18:10: error: cannot bind 'int' lvalue to 'int&&'
int &&q=p; // Only valid with std::move
Arithmetic Operations
In C++, arithmetic operations can be applied to the pointers named "Pointer Arithmetic". On the other hand, arithmetic operations cannot be applied to the references. There is no word, i.e., Reference Arithmetic exists in C++ .
Difference between Reference and Pointer in Tabular Form
Here, we will discuss several differences between references and pointers in tabular form in C++.
| Features | Reference | Pointer |
|---|---|---|
| Definition | It represents an alias for an existing variable. | It is a variable that contains the memory address of another variable. |
| Syntax | int &ref = var; | int *ptr = &var; |
| Nullability | It cannot be null. | It can be null. |
| Reassignment | Reference cannot be reassigned to another variable. | Pointers can be reassigned to point to different variables. |
| Arguments | The reference is passed using pass-by-reference semantics. | Pointers can simulate pass-by-reference by passing the address of a variable. |
| Memory Address | It is used to share the same address just like the original value. | It contains their own memory address. |
| Indirection | It only provides one level of indirection. | It provides extra levels of indirections. |
Conclusion
In conclusion, C++ references and pointers both provide several methods to access and manipulate data indirectly, but they can be served for different purposes. References are easy to understand and simple, which makes them suitable for parameter passing and aliasing.
On the other hand, pointers provide greater flexibility, which enables null values, reassignment, and dynamic memory handling. We can choose these methods based on our requirements. References are suitable when simplicity and safety are required, and pointers when more control and dynamic behaviour are required.
Difference between Reference and Pointer in C++ FAQs
1) What is the main difference between a reference and a pointer in C++?
The main difference between a reference and a pointer is that a reference is an alias for a variable, whereas a pointer holds the variable memory address.
2) Can Pointers be reassigned in C++?
Yes, pointers may be reassigned to point to different variables in C++.
3) Are references providing more safety than pointers in C++?
Yes, references provide more safety than pointers because they should be initialized and cannot be null.
4) Can a reference be Null like a pointer in C++?
No, the reference should refer to a valid variable. Pointers can be Null.
5) Do pointers require dereferencing to access the value in C++?
Yes, pointers need to use the * operator to access the value.