Uses and differences:reference and pointers.


Reference and pointers has it’s own uses and differences.There are times when reference is preferred over pointers and some other time pointers over reference.In this post let us see some of their general and specific uses and also their differences.

General uses of reference and pointers

The general use of reference and pointers is to change the value of a variable outside it’s scope.When a value is passed to the function using either a reference or a pointers.And if any changes is made to the variable by using it’s reference or pointers then the change will be seen even outside it’s scope.This is not true in case of pass by value.Any changes made using the argument in pass by value last only inside that function or scope.

void func(int *ptr , int &ref , int zArg) /* the arguments are pointers,reference and variable */
{
*ptr = 100 ;
ref = 99 ;
zArg = 156 ;

cout<< “\nInside func() ” << endl ;
cout << “*ptr=” << *ptr << “,” << “ref=” << ref << “,” << “zArg=” << zArg << endl ;
}

int main( )
{
int x = 55 , y = 27 , z = 75 ;
int *ptr = &x ;
cout << “x=” << x << “,*ptr=” << *ptr << endl ;
cout<< “y=” << y << endl ;
cout<< “z=” << z << endl ;

func( ptr , y , z ) ; /* Passed by address , reference and variable */

cout<< “\nAfter returning from func() \n” ;
cout << “x=” << x << “,*ptr=” << *ptr << endl ;
cout<< “y=” << y << endl ;
cout<< “z=” << z << endl ;

cin.get() ;
return 0 ;
}

The output is,

x = 55 , *ptr = 55
y = 27
z = 75

Inside func()
*ptr= 100 , ref = 99 , zArg = 156 ;

After returning from func()
x = 100 ,*ptr = 100
y = 99
z = 75

You can see that changing the value of zArg inside func() does not change the value of z.But for the variable x and y the changes is seen even inside the main() function.So why does this difference arises? the explanation is provided below.

In calling the func(),the variable ‘z’ is passed normally,also known as pass by value.The variable z is received by the parameter zArg in func().The storage of z and zArg is completely different.The storage of zArg is a temporary storage and the value of z is copied to it for use by the func() function.In changing the zArg value inside the func() only the value in the temporary storage is changed,any changes is not made in the real storage of z.So changing zArg value does not affect the value of z in main() function.

But for the variables x and y we have passed them as a reference and a pointer.Reference and pointer parameters does not necessitate the creation of any temporary storage.So,in changing the value using the reference or a pointer we are directly changing the value in the storage of x and y.Hence the changes is seen inside the main() function or outside the func().





Specific uses of reference

i)Use reference for all predefined variable i.e. variables created in stack.

int i=89 ;
int &ri=i ; ///works fine and preferred
int *ip=&i ; ///Works fine

 
ii)If passing pointer is necessary then use reference instead.The syntax is simpler and less confusing.

void func( string &s ) ///Simpler syntax
{
cout<< s ;
}

void func1( string *s )
{
cout<< *s ;
}

int main( )
{
string str ;
func(str) ; ///passing reference
func1( &str ) ; ///passing pointer

string *stp=&str ;
func1( stp ) ;   /*Passing pointer-works fine but require extra declaration of string pointers */

cin.get() ;
return 0 ;
}

 

Specific uses of pointers

i)Pointers are used during dynamic allocation of memory or allocating memory during run-time using new operator.

ii)Pointers are used in shared_ptr and smart pointers(discussed in C++11 features).

iii)Use pointers if you are returning a null pointer.

iv)Pointers are also used in linked lists,stack,binary tree,etc, which may require allocation of memory during run-time.


 


Stop wasting time,earn money($$$) from your website-Join Now!

Differences between reference and pointers.

Difference 1: A reference which has referred to one variable cannot refer to another variable which means a reference is bound to one variable in it’s life time.Whereas a pointer can point to one or more variable in it’s life time.

/*** reference ***/
int i=9,ii=89 ,&ri=i ;
&ri=ii ;   ///error:ri is bound only to i

/*** pointer ***/
int *ip=&i ;
ip=&ii ;   //Works fine

Difference 2: There is no such thing as null reference but null pointers exist.

/*** reference ***/
int &ri=nullptr ; ///Error

/*** pointers ***/
int *ip=nullptr ; ///works fine

Difference 3: A reference cannot be of void type but a pointer can.

/*** reference ***/
int i ;
void &ri=i ; ///Error

/*** pointers ***/
int i ;
void *ip=&i ; ///works fine