Double pointer vs pass by reference pointer

Understanding the concept of a double pointer and where it should be used, I doubt it. I experimented with this code and found that I can use a pointer following a link instead of two 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 is the advantage of using double pointers instead of links? And where should this thing be used

+3
source share
4 answers

"Why use a link to a pointer instead of a pointer to a pointer?" You will get the same answer as the request β€œwhy use a pointer instead of a link” for any other variable ...

Mostly:

  • references (to a pointer or any other variable) are smart because an object must always exist

  • pointers (for a pointer or any other variable) are smart because they can be NULL (optional)

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

  • references (to a pointer or any other variable) are smart because they can be used as objects (there is no need for dereferencing, like pointers, simpler syntax, radiation)

  • etc...

There are already many posts that answer this question:

What is the difference between a pointer variable and a reference variable in C ++?

Are there any advantages of passing a pointer to passing by reference in C ++?

+2
source

In this case, the double pointer idiom is an inheritance of C. C did not have a notion of references (and still does not exist), so the only way to change the pointer is to pass the pointer to the pointer.

But since C ++ offers links, you should use them when you only need to change a variable in a function, whether it be a pointer or not, as this allows the code to be more understandable about the programmer's intentions.

The exception must be higher than the rule if you want to use null as a special case (a link can never point to null)

+1
source

The pointer-to-pointer idiom goes back to C, where it was needed. C had no links. You are right in suspecting that this is not necessary in C ++.

(A "double pointer" can also refer to double* , BTW, a pointer to a pointer is not so ambiguous)

0
source

There are no advantages from each other. They both satisfy the same needs. Therefore, in the end, it all comes down to preference.

Some argue that a pointer to a pointer is safer than a pointer to a pointer.

-1
source

Source: https://habr.com/ru/post/956135/


All Articles