Prerequisite: Pointers vs References in C++.
For clear understanding, let’s compare the usage of a “pointer to pointer” VS “Reference to pointer” in some cases.
Note: It is allowed to use “pointer to pointer” in both C and C++, but we can use “Reference to pointer” only in C++.
Passing pointer to a function
If a pointer is passed to a function as a parameter and tried to be modified then the changes made to the pointer does not reflects back outside that function. This is because only a copy of the pointer is passed to the function. It can be said that “pass by pointer” is passing a pointer by value. In most cases, this does not present a problem. But the problem comes when you modify the pointer inside the function. Instead of modifying the variable, you are only modifying a copy of the pointer and the original pointer remains unmodified.
Below program illustrate this:
Passing Pointer to function: Before :23 After :23
Passing “pointer to a pointer” as a parameter to function
The above problem can be resolved by passing the address of the pointer to the function instead of a copy of the actual function. For this, the function parameter should accept a “pointer to pointer” as shown in the below program:
Passing a pointer to a pointer to function Before :23 After :42
How to call a function with “Reference to pointer” parameter?
A reference allows called function to modify a local variable of the caller function. For example, consider the following example program where fun() is able to modify local variable x of main().
New value of x is 20
Below program shows how to pass a “Reference to a pointer” to a function:
Passing a Reference to a pointer to function Before :23 After :42
Returning a pointer from a function
Return a pointer from a function Before :23 After :42
Returning reference from function
Returing a Reference Before :23 After :42
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Passing by pointer Vs Passing by Reference in C++
- Different ways to use Const with Reference to a Pointer in C++
- Reference to a pointer in C++ with examples and applications
- When do we pass arguments by reference or pointer?
- How to Copy Struct Type Using Value and Pointer Reference in Golang?
- Double Pointer (Pointer to Pointer) in C
- Output of the program | Dereference, Reference, Dereference, Reference....
- What is a Pointer to a Null pointer
- Passing and Returning Objects in C++
- Passing NULL to printf in C
- Parameter Passing Techniques in C/C++
- How to delete an element from the Set by passing its value in C++
- Passing a vector to constructor in C++
- Passing vector to a function in C++
- Message Passing in Java
- Perl | Passing Complex Parameters to a Subroutine
- How to pass PHP Variables by reference ?
- Why do we need reference variables if we have pointers
- Perl | Pass By Reference
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai