Difference Between Pointer Variable and Reference Variable

Difference between Pointer variable and Reference variable






Difference between Pointer variable and Reference variable.

Pointer Variable
Reference Variable
1. It is a special variable which can store
     the address of another variable
It is a duplicate name (alias) given to any variable.
2. Here the Pointer variable will be created 
    in the memory and then it can be  
    assigned any address.
No extra space is required. Only a duplicate name is assigned to the same memory area.
3. Using  * or de-referencing operator 
    along with the pointer variable the 
    value inside the address can be
    accessed
By simply using an  =   or assignment operator with the reference variable the value can be changed
4. Used while implementing Call By
    Address
Used while implementing Call By Reference
5. Common Syntax :-
   Declaration :-  
   Data type   *Pointer variable name ;
   Acquiring address :-
   Pointer variable = & variable name ;
   Here the address of the ‘ variable ‘ would 
   be stored get stored in the ‘ pointer 
   variable ‘.
   Manipulating data :-
   *Pointer variable = value;
   here the ‘ value ‘ would be assigned to 
   the   ‘ variable name ‘.
Common Syntax :-
Declaration :- 
Data type   & reference = variable ;
Here the LHS ‘ reference ‘ name given would become the duplicate name for the
‘ variable ‘ in the RHS
Manipulating data :-
reference = value ;
here the ‘ value ‘ would be assigned to the ‘ variable  ‘, which would be reflected in both the reference as well as the original  
 ‘ variable’.
6. Example:-
    int a = 10 ;
    int *p ;
    cout<< a ; // it would show 10
    p = &a;
    *p = 20;
    cout<< a ; // it would show 20
Example :-
int a = 10;
int &b = a;
cout << a << b ; // It would show 10  10
b=20;
cout << a << b ; // It would show 20  20

Labels: