1

These two function do exactly the same thing.

But in one the function argument is pointer to pointer and in the second it is reference to pointer.

1) Is there any difference between the two

2) when to choose which one.

#include <iostream> 

using namespace std; 

int global_var = 42; 

// function to change pointer to pointer value 
void changePointerValue(int** ptr_ptr) 
{ 
    *ptr_ptr = &global_var; 
} 

int main() 
{ 
    int var = 23; 
    int* pointer_to_var = &var; 

    cout << "Passing a pointer to a pointer to function " << endl; 

    cout << "Before :" << *pointer_to_var << endl; // display 23 

    changePointerValue(&pointer_to_var); 

    cout << "After :" << *pointer_to_var << endl; // display 42 

    return 0; 
}

/////////////////////////////////////////////////////////////////////

#include <iostream> 

using namespace std; 

int gobal_var = 42; 

// function to change Reference to pointer value 
void changeReferenceValue(int*& pp) 
{ 
    pp = &gobal_var; 
} 

int main() 
{ 
    int var = 23; 
    int* ptr_to_var = &var; 

    cout << "Passing a Reference to a pointer to function" << endl; 

    cout << "Before :" << *ptr_to_var << endl; // display 23 

    changeReferenceValue(ptr_to_var); 

    cout << "After :" << *ptr_to_var << endl; // display 42 

    return 0; 
} 
  • "Difference between pointer to pointer and reference to pointer" Do you mean besides the fact that they are different types? – Some programmer dude Sep 12 '19 at 06:45
  • @Some programmer dude yes when do i choose to use which one. –  Sep 12 '19 at 06:46
  • 2
    In practical terms the only difference is that a reference can never be "null", it is guaranteed (unless you do some hack to workaround it) to point to a valid instance. Pointers are just memory addresses, they can be pointing to something valid, or be `nullptr`, or just have some arbitrary address. – Havenard Sep 12 '19 at 06:47
  • That's really hard to give an answer to, as it will depend on use-case and context. Is this just some curiosity from your side, or do you have another problem that you need to solve? Perhaps you should ask directly about that other problem instead? – Some programmer dude Sep 12 '19 at 06:47
  • Essentially, no, pointers and references in usage are very similar, except you can do more with a pointer, a reference is safer and more restrictive. – SPlatten Sep 12 '19 at 06:48
  • @Some programmer dude this is just curiosity i wanted to know if there might be any case where we should prefer one over another. –  Sep 12 '19 at 06:49
  • 2
    When you see a pointer, you better make sure it is not null before you access the pointed-to object. When you have reference, you can be sure that it points to a valid object (unless someone made a terrible mistake before they passed the reference to your code). Seen from the other side. When my function always needs a valid object, I have it accept reference. When my function makes sense to be given an object or not, then I make it accept a pointer. – slepic Sep 12 '19 at 06:55
  • 5
    The difference is exactly the same as that between pointers and references to any other type. There is nothing special about pointers at all. – molbdnilo Sep 12 '19 at 07:02

0 Answers0