Javatpoint Logo
Javatpoint Logo

C++ Reference vs Pointer

C++ reference and pointer seem to be similar, but there are some differences that exist between them. A reference is a variable which is another name of the existing variable, while the pointer is variable that stores the address of another variable.

What is Reference?

A reference is a variable that is referred to as another name for an already existing variable. The reference of a variable is created by storing the address of another variable.

A reference variable can be considered as a constant pointer with automatic indirection. Here, automatic indirection means that the compiler automatically applies the indirection operator (*).

Example of reference:

In the above declaration, 'a' is an alias name for 'i' variable. We can also refer to the 'i' variable through 'a' variable also.

Let's understand through an example.

In the above code, 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 Pointer?

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.

Differences between Reference and Pointer

The following are the differences between reference and pointer:

  • Definition

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.

Let's understand through a simple example.

Output:

Value of 'a' is :10                                                                                                             
Now value of 'a' is :8  

Whereas, 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

We can declare a reference variable by adding a '&' symbol before a variable. If this symbol is used in the expression, then it will be treated as an address operator.

Before using a pointer variable, we should declare a pointer variable, and this variable is created by adding a '*' operator before a variable.

  • Reassignment

We cannot reassign the reference variable. Now, we take a simple example as given below:

The above code shows the error that multiple declarations of int &a are not allowed. Therefore, the above program concludes that reassignment operation is not valid for the reference variable.

Whereas, the pointers can be re-assigned. This reassignment is useful when we are working with the data structures such as linked list, trees, etc.

  • Memory Address

In the case of reference, both the reference and actual variable refer to the same address. The new variable will not be assigned to the reference variable until the actual variable is either deleted or goes out of the scope.

Let's understand this scenario through an example.

Output:

The address of 'a' variable is : 0x7fff078e7e44                                                                        The address of 'i' variable is : 0x7fff078e7e4

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. Let's understand this through an example.

Output:

The memory address of p variable is :0x7ffcc5c164b8                                                           The memory address of k variable is :0x7ffcc5c164b4 
  • NULL value

We cannot assign the NULL value to the reference variable, but the pointer variable can be assigned with a NULL value.

  • Indirection

Pointers can have pointer to pointer offering more than one level of indirection.

In the above code, the pointer 'p' is pointing to variable 'a' while 'q' is a double pointer which is pointing to 'p'. Therefore, we can say that the value of 'p' would be the address of 'a' variable and the value of 'q' variable would be the address of 'p' variable.

Output:

The value of q is : 0x7ffd104891dc  

In the case of References, reference to reference is not possible. If we try to do c++ program will throw a compile-time error

Let's understand this scenario through an example.

Output:

main.cpp: In function 'int main()':
main.cpp:18:10: error: cannot bind 'int' lvalue to 'int&&'
int &&q=p;
  • Arithmetic Operations

As we know that arithmetic operations can be applied to the pointers named as "Pointer Arithmetic", but arithmetic operations cannot be applied on the references. There is no word, i.e., Reference Arithmetic exists in C++.

Let's see a simple example of Pointers.

Output:

The value of *ptr is :1                                                                                                       
The value of *ptr is: 2

Let's understand the references through an example.

The above code will throw a compile-time error as arithmetic operations are not allowed with references.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA