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
|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 '&'.|
|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.
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.
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.
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.
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
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.
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
- Reference is like creating an another name to refer a variable so that it can be refered with different names. On the other hand, a pointer is simply a memory address of a variable.
- A pointer variable if preceeded by ‘*’ returns the value of a variable whose address is stored in the pointer varaible. A reference variable when preceeded by ‘&’ returns the address of that variable.
- Pointer operators are * and -> whereas, reference operator is &.
- A pointer variable if does not carry any variable’s address it points to null. On the other hand, a reference variable can never refer to Null.
- You can always create an unitialized pointer variable, but we create a reference when we need an alias of some variable so you can never create an unitialize refernce.
- You can reinitialize a pointer but once you initialize arefernce you can not reinitialize it again.
- You can create an empty pointer and initialize it at any time but you have to initialize refrence only when you create a refernce.
The pointer and reference both are used to point or refer another variable. But both differ in their usage and implementation.