Difference between passing pointer to pointer and address of pointer to any function - GeeksforGeeks

26 Apr, 2021
In this article, the differences between passing “pointer to pointer” and “address of pointer” to a function. In C or C++ Programming Language, it is known that pointers hold the address of the variables or any memory location. If pointers are pointed to the memory location, it can be used to change the value of the variable.
As for functions, any pointer can be passed by itself, or by the address of the pointer. But If the memory location of the pointer is to be changed permanently out of the function, it must be captured with a reference or double-pointer within the function i.e., with the assumption any memory is created inside the local function.
Afterward, it is pointed to the pointer captured by the function without any reference or any double-pointer. In this situation, there is no change inside the main function because it was not captured by reference or double-pointer inside the function definition.
Program 1:
C++
// C++ program to illustrate the// concepts pointers  #include <iostream>#include <stdlib.h>using namespace std;  // Function to assign the value of// pointer ptr to another locationvoid foo(int* ptr){    int b = 2;      // ptr= (int*)malloc(sizeof(int));    // It can be executed instead of    // the ptr=&b but nothing change    ptr = &b;}  // Driver Codeint main(){    int a = 5;    int* ptr = &a;      cout << "Before the function ptr: "         << *ptr << endl;    foo(ptr);      cout << "After the function ptr: "         << *ptr << endl;      return 0;}
Before the function ptr: 5
After the function ptr: 5

Explanation: The reason why there is no change in the output is that it is created a copy pointer for the function. So, the change in the address of the pointer does not reflect out of the function. References or double-pointer can be used for this.
Program 2:
C++
// C++ program to illustrate the// concepts of pointer  #include <iostream>#include <stdlib.h>using namespace std;  // Function to change the pointer ptrvoid foo(int* ptr){    int b = 2;      // The change will only be for    // the new value of the ptr    *ptr = b;}  // Driver Codeint main(){      int a = 5;    int* ptr = &a;      cout << "Before the function, "         << "new value of the ptr: "         << *ptr << endl;      // Function Call    foo(ptr);      cout << "After the function, "         << "new value of the ptr: "         << *ptr << endl;      return 0;}
Before the function, new value of the ptr: 5
After the function, new value of the ptr: 2

Explanation:
If it is wanted to replace the memory stored in ptr when the foo() function is complete, ptr must be captured with the reference or double-pointer in the foo() function.
Program 3:
C++
// C++ program to illustrate the// concepts of pointer#include <iostream>#include <stdlib.h>using namespace std;  // Function to change the value of// the pointers ptr1void foo(int** ptr1){    int b = 2;      // ptr = &b;    // For this assignment, the    // captured value will be like    // void foo(int* &ptr)    *ptr1 = &b;}  // Driver Codeint main(){    int a = 5;    int* ptr = &a;      cout << "Before the function, "         << "new value of the ptr: "         << *ptr << endl;      // Function Call    foo(&ptr);      cout << "After the function, "         << "new value of the ptr: "         << *ptr << endl;      return 0;}
Before the function, new value of the ptr: 5
After the function, new value of the ptr: 2

Explanation:
CPP-Basics
Memory Management
Pointers
C++
C++ Programs
Difference Between
Pointers
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operator Overloading in C++
Polymorphism in C++
Friend class and function in C++
Sorting a vector in C++
std::string class in C++
Header files in C/C++ and its uses
Program to print ASCII Value of a character
C++ Program for QuickSort
How to return multiple values from a function in C or C++?
Sorting a Map by value in C++ STL