20 Apr 2017

Difference Between Pointer and Reference


pointer and referenceThe “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

  1. Comparison Chart
  2. Definition
  3. Key Differences
  4. Conclusion

Comparison Chart

BASIS FOR COMPARISONPOINTERREFERENCE
BasicThe pointer is the memory address of a variable.The reference is an alias for a variable.
ReturnsThe 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 ReferenceThe pointer variable can refer to NULL.The reference variable can never refer to NULL.
InitializationAn uninitialized pointer can be created.An uninitialized reference can never be created.
Time of InitializationThe 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.
ReinitializationThe 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.
  1. type *var_name;
Let us understand pointer with the help of an example.
  1. int a=4;
  2. int * ptr= &a;
  3. cout<<a; //4
  4. cout<<ptr; //2007 address of varaible a
  5. 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.
  1. type **var_name;
Let’s study it with an example.
  1. int a=4;
  2. int * ptr1= &a;
  3. int ** ptr2= &ptr1;
  4. cout<<a; //4
  5. cout<<ptr1; //2007 address of varaible a
  6. cout<<*ptr1; //4
  7. cout<<ptr2; //1007 address of pointer variable ptr1.
  8. 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.
  1. 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.
  1. int a = 4;
  2. int &b = a; // b refers to a
  3. 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

  1. 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.
  2. 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.
  3. Pointer operators are * and -> whereas, reference operator is &.
  4. 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.
  5. 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.
  6. You can reinitialize a pointer but once you initialize arefernce you can not reinitialize it again.
  7. You can create an empty pointer and initialize it at any time but you have to initialize refrence only when you create a refernce.
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.

No comments:

Post a Comment

commnet here