Here you will learn about difference between reference and pointer in C++.
Reference: Reference contains address of a variable. It can be called as alternative name of a variable.
Example:
int i; int &ri = i;
Pointer: Pointer also contains address of a variable.
Example:
int i; int *pi = &i;
Both reference and pointer are used to point to address of a variable but they are different in many ways.
Difference between Reference and Pointer in C++
S.No | Reference |
Pointer |
1. | Once reference is assigned an address then it can’t be reassigned address of any other variable. | There is no such restriction. |
2. | Reference can’t be NULL. | Pointer can be NULL. |
3. | A reference should be initialized at the time of its declaration. | There is no such restriction. |
4. | We can dereference the address without using any operator. | In pointer, address is dereferenced using * operator. |
5. | We can’t increment or decrement the address stored in reference. Due to this, reference is safe to use. | It is possible to increment or decrement the address stored in pointer. Due to this, pointer is not safe to use. |
Comment below if you found any mistake or know any other difference between pointer and reference in C++.