Difference Between Pointer and Reference (with Comparison Chart)
The “pointer” and “reference” both are used to point or refer an another variable. But, the basic difference among both of them is that a pointer variable points to a variable whose memory location is stored in it. The reference variable is an alias for a variable which is assigned to it.
The comparison chart below explores the other differences between a pointer and a reference.
Content: Pointer Vs Reference
Comparison Chart
Basis For Comparison | Pointer | Reference |
---|---|---|
Basic | The pointer is the memory address of a variable. | The reference is an alias for a variable. |
Returns | The pointer variable returns the value located at the address stored in pointer variable which is preceded by the pointer sign '*'. | The reference variable returns the address of the variable preceded by the reference sign '&'. |
Operators | *, -> | & |
Null Reference | The pointer variable can refer to NULL. | The reference variable can never refer to NULL. |
Initialization | An uninitialized pointer can be created. | An uninitialized reference can never be created. |
Time of Initialization | The pointer variable can be initialized at any point of time in the program. | The reference variable can only be initialized at the time of its creation. |
Reinitialization | The pointer variable can be reinitialized as many times as required. | The reference variable can never be reinitialized again in the program. |
Definition of Pointer
A “pointer” is a variable that holds the memory location of another variable. The operators used by the pointer variable is * and ->. The declaration of pointer variable contains the base data type followed by the ‘*’ sign and the variable name.
type *var_name;
Example
Let us understand pointer with the help of an example.
int a=4; int * ptr= &a; cout<<a; //4 cout<<ptr; //2007 address of varaible a cout<<*ptr; //4
Here, we have an integer variable a and, a pointer variable ptr which stores the address of variable a.
Pointer Arithmetic
The pointer variable can be operated with two arithmetic operators that are “addition” and “subtraction”. The addition referred as “increment”, and the subtraction is referred as “decrement”. As a pointer variable is incremented, it points to the memory location of the next variable of its base type. As a pointer variable is decremented, it points to the memory location of the previous variable of its base type. Hence, an array can be efficiently accessed by a pointer variable.
Multiple Indirection
A pointer points to the other pointer variable which is pointing to the target value. This kind of pointer is always initialized with the address of another pointer variable. The declaration of a pointer to a pointer is as follow.
type **var_name;
Let’s study it with an example.
int a=4; int * ptr1= &a; int ** ptr2= &ptr1; cout<<a; //4 cout<<ptr1; //2007 address of varaible a cout<<*ptr1; //4 cout<<ptr2; //1007 address of pointer variable ptr1. cout<<**ptr2; //4
Function pointer
As we know that a function is not a variable, still it has a memory location, that can be assigned to a pointer variable. Once a pointer points to a function, then the function can be called with that function pointer.
The important points to remember about the pointer.
- The pointer variable can be created without its initialization, and it can be initialized anywhere in the program.
- The pointer variable can be reinitialized to an another variable.
- The pointer variable can refer to NULL.
Definition of Reference
The reference variable is used to refers to the variable which is assigned to that reference variable. The operator used by the reference variable is ‘&’. The declaration of a reference variable contains base type followed by ‘&’ sign and then variable name.
type & refer_var_name = var_ name;
Here, the type is the datatype, the & operator confirms that it is a reference variable. The refer_var_name is the name of the reference variable. The var_name is the name of the variable, which we want the reference variable to refer.
Example
Let us understand the reference variable with the help of an example.
int a = 4; int &b = a; // b refers to a b = 6; // now a = 6
Here, the variable of type int is assigned a value 4. The reference variable is assigned the variable a, i.e. b is alias of a. Now, when we assign another value to b, we modify the value of a. Hence, it can be said that the changes done to a reference variable will also occur in variable referred by that reference variable.
The most important point is that the reference variable must be initialized at the time of its creation.Once the reference variable is initialized with a variable, it can not be reinitialized to refer an another variable. The moment you assign a value to a reference variable, you assign that value to a variable that a reference variable points to. The reference variable can never refer to NULL. Arithmetic can not be performed on a reference variable.
The reference variable can be used in three ways:
- As a function return value.
- As a function parameter.
- As a stand alone reference.
Key Differences Between Pointer and Reference
Note
Java does not support pointers.
Conclusion
The pointer and reference both are used to point or refer another variable. But both differ in their usage and implementation.
ncG1vNJzZmislZi1pbXFn5yrnZ6YsrR6wqikaJyZm7OmvsSnmp5lkprBuLHEp2Spp5mjwaa%2BjJqlnWWimrOmvsSnmp5lYmO1tbnL