Skip to content
Related Articles

Related Articles

Difference between passing pointer to pointer and address of pointer to any function
  • Last Updated : 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 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;
  
// Function to change the value of
// the pointers ptr1
void foo(int** ptr1)
{
    int b = 2;
  
    // ptr = &b;
    // For this assignment, the
    // captured value will be like
    // void foo(int* &ptr)
    *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;
}
Output:
Before the function, new value of the ptr: 5
After the function, new value of the ptr: 2

Explanation:

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for the language and STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :