While understanding the double pointer concept and where it should be used,I have a doubt.I had experimented this code and found that i could use pointer passing by reference also instead of double pointers.
#include<iostream>
using namespace std;
void modify_by_value(int* );
void modify_by_refrence(int* &);
int a=4, b=5;
void main()
{
int *ptr = NULL;
ptr = &a;
cout << "*ptr before modifying by value: " << *ptr << endl;
modify_by_value(ptr);
cout << "*ptr after modifying by value: " << *ptr << endl;
cout << "*ptr before modifying by refrence: " << *ptr << endl;
modify_by_refrence(ptr);
cout << "*ptr after modifying by refrence: " << *ptr << endl;
}
void modify_by_value(int* ptr) //this function can change *ptr but not the ptr(address contained) itself;
{
ptr = &b;
}
void modify_by_refrence(int * &ptr) //this function has refrence and hence can modify the pointer;
{
ptr = &b;
}
What's the benefit of using double pointers instead of reference?And where this thing should be used