Difference between passing pointer to pointer and address of pointer to any function
Last Updated :
01 Nov, 2022
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 a 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 location
void 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 Code
int 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;
}
Output: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 ptr
void foo(int* ptr)
{
int b = 2;
// The change will only be for
// the new value of the ptr
*ptr = b;
}
// Driver Code
int 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;
}
Output: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;
int b = 2;
// Function to change the value of
// the pointers ptr1
void foo(int** ptr1) { *ptr1 = &b; }
// Driver Code
int 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;
}
OutputBefore the function, new value of the ptr: 5
After the function, new value of the ptr: 2
Explanation:

Similar Reads
Difference Between Pointers and Array Notations in C++ In C++, pointers and array notations are two ways using which we work with arrays and memory for accessing the data. They have distinct behaviours and are used in different contexts. In this article, we will learn the key differences between pointers and array notations in C++. Difference Between Po
4 min read
Difference between constant pointer, pointers to constant, and constant pointers to constants In this article, we will discuss the differences between constant pointer, pointers to constant & constant pointers to constants. Pointers are the variables that hold the address of some other variables, constants, or functions. There are several ways to qualify pointers using const. Pointers to
3 min read
How to Create a Pointer to a Function in C++? In C++, a function pointer is a variable that stores the address of a function that can later be called through that function pointer. It is useful for passing functions as parameters to other functions(callback functions) or storing them in data structures. In this article, we will learn how to use
2 min read
Difference between int (*p)[3] and int* p[3]? Pointers store the address of variables or a memory location. Pointers are a symbolic representation of addresses. They enable programs to simulate call-by-reference as well as to create and manipulate dynamic data structures. Its general declaration in C/C++ has the format: Syntax: datatype *var_na
2 min read
How to Return a Pointer from a Function in C++? In C++, we can return a pointer from a function which is useful when we want to return large data structures that cannot be returned by value. However, it must be done carefully to avoid memory leaks, dangling pointers, and other issues related to dynamic memory management. In this article, we will
2 min read