9

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

Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
Omkar
  • 791
  • 1
  • 9
  • 26
  • 1
    You should use the one that more clearly reflects the intent of the code. – David Schwartz May 02 '16 at 07:04
  • @DavidSchwartz if the intent is to modify the value of a pointer, it doesn't seem obvious which to pick.. at least presumably not to omkar – xaxxon May 02 '16 at 07:05
  • As seen by the answers, this is a "primarily opinion" question and should be closed as such. – xaxxon May 02 '16 at 07:10
  • @xaxxon Since he picked the one that I think best reflects the intent of the code in both cases, I think it probably does seem obvious to him. In both his examples, neither `&` nor `&` are needed either at the call site or on the left side of the assignment in the function. Whereas, had he made the other choice in either case, they would be. – David Schwartz May 02 '16 at 07:13
  • 1
    See http://stackoverflow.com/questions/7058339/when-to-use-references-vs-pointers?rq=1 – Klas Lindbäck May 02 '16 at 07:34
  • @xaxxon as already stated out in the question 'was experimenting with this code' the intent didn't really mattered me as i was doing the same thing in both the cases..And yes,I accept it could be a duplicate,because I was searching for the keyword double pointer. – Omkar May 02 '16 at 07:41

4 Answers4

5

"Why using reference to pointer instead of pointer to pointer"? You will get the same answer as if asking "why using pointer instead of reference" for any other kind of variable...

Basically:

  • references (to pointer or any other variable) are smart because there should always be an object behind

  • pointers (to pointer or any other variable) are smart because they could possibly be NULL (optional)

  • references (to pointer or any other variable) are not available in C

  • references (to pointer or any other variable) are smart because they can be used as objects (no need to dereference like pointers, easier syntax, rading)

  • etc...

There are many posts answering this question already:

What are the differences between a pointer variable and a reference variable in C++?

Are there benefits of passing by pointer over passing by reference in C++?

Community
  • 1
  • 1
jpo38
  • 20,821
  • 10
  • 70
  • 151
1

In that case the double pointer idiom is a C inheritance. C had no concept of references (and still has none) so the only way to modify a pointer was to pass a pointer to pointer.

But as C++ offers references, you should use them when you only need to change a variable in a function, be it a pointer or not as it allow code to be more clear about programmer intentions.

The exception to be above rule if when you want to use the null value as a special case (a reference can never point to null)

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
0

The "pointer to pointer" idiom dates back to C, where it was necessary. C didn't have references. You're right to suspect it's not needed in C++.

(A "double pointer" might also refer to double*, BTW, pointer-to-pointer isn't so ambiguous)

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • how would you pass the address of a pointer to a function that could possibly be nullptr without a double pointer? Or if you want to change the value of a double pointer - that you've allocated an array of single pointers for it to point to. – xaxxon May 02 '16 at 07:05
  • @xaxxon: `optional` ; come C++17 that will be `std::optional`. Can't be confused with `T*&` – MSalters May 02 '16 at 07:09
0

There are no benefits of one over the other. They both satisfy the exact same needs. So in the end it all comes down to preference.

Some argue that reference-to-pointer is safer than pointer-to-pointer.

Zaid Amir
  • 4,727
  • 6
  • 52
  • 101