Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
Passing Reference to a Pointer in C++
Next article icon

Passing Reference to a Pointer in C++

Last Updated : 09 Dec, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report
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: CPP
#include <iostream>  using namespace std;  int global_Var = 42;  // function to change pointer value void changePointerValue(int* pp) {     pp = &global_Var; }  int main() {     int var = 23;     int* ptr_to_var = &var;      cout << "Passing Pointer to function:" << endl;      cout << "Before :" << *ptr_to_var << endl; // display 23      changePointerValue(ptr_to_var);      cout << "After :" << *ptr_to_var << endl; // display 23      return 0; } 
Output:
  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: CPP
#include <iostream>  using namespace std;  int global_var = 42;  // function to change pointer to pointer value void changePointerValue(int** ptr_ptr) {     *ptr_ptr = &global_var; }  int main() {     int var = 23;     int* pointer_to_var = &var;      cout << "Passing a pointer to a pointer to function " << endl;      cout << "Before :" << *pointer_to_var << endl; // display 23      changePointerValue(&pointer_to_var);      cout << "After :" << *pointer_to_var << endl; // display 42      return 0; } 
Output:
  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(). CPP
#include<iostream>  using namespace std;  void fun(int &x) {      x = 20;  }     int main() {      int x = 10;      fun(x);      cout<<"New value of x is "<<x;      return 0;  } 
Output:
  New value of x is 20  
Below program shows how to pass a "Reference to a pointer" to a function: CPP
#include <iostream>  using namespace std;  int gobal_var = 42;  // function to change Reference to pointer value void changeReferenceValue(int*& pp) {     pp = &gobal_var; }  int main() {     int var = 23;     int* ptr_to_var = &var;      cout << "Passing a Reference to a pointer to function" << endl;      cout << "Before :" << *ptr_to_var << endl; // display 23      changeReferenceValue(ptr_to_var);      cout << "After :" << *ptr_to_var << endl; // display 42      return 0; } 
Output:
  Passing a Reference to a pointer to function  Before :23  After :42  

Returning a pointer from a function

CPP
#include <iostream>  using namespace std;  int global_var = 42;  // function to return a pointer int* returnPointerValue() {     return &global_var; }  int main() {     int var = 23;     int* ptr_to_var = &var;      cout << "Return a pointer from a function " << endl;      cout << "Before :" << *ptr_to_var << endl; // display 23      ptr_to_var = returnPointerValue();      cout << "After :" << *ptr_to_var << endl; // display 42      return 0; } 
Output:
  Return a pointer from a function   Before :23  After :42  

Returning reference from function

CPP
#include <iostream>  using namespace std;  int global_var = 42;  // function to return reference value int& ReturnReference() {     return global_var; }  int main() {     int var = 23;     int* ptr_to_var = &var;      cout << "Returning a Reference " << endl;      cout << "Before :" << *ptr_to_var << endl; // display 23      ptr_to_var = &ReturnReference();      cout << "After :" << *ptr_to_var << endl; // display 42      return 0; } 
Output:
  Returning a Reference   Before :23  After :42  

Next Article
Passing Reference to a Pointer in C++

H

HamdyFouad
Improve
Article Tags :
  • C++ Programs
  • C++
Practice Tags :
  • CPP

Similar Reads

    Different ways to use Const with Reference to a Pointer in C++
    Before moving forward with using const with Reference to a Pointers, let us first see what they are one by one: Pointers are used to store the address of variables or a memory location. A variable can be declared as a pointer by putting ‘*’ in the declaration. datatype *var_name; Example: CPP // C++
    5 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
    How to add reference of an object in Container Classes
    We all are familiar with an alias in C++. An alias means another name for some entity. So, a reference variable is an alias that is another name for an existing variable/object etc. Below is the program for adding reference to a variable: CPP // C++ program to illustrate // aliasing in variable #inc
    4 min read
    Overloads of the Different References in C++
    This article focuses on function/method overloads by references, as well as the types of arguments that can be passed. Prerequisites: l-value references.r-value references.Move semantics - std::move(). Overview:l-value refers to a memory location that identifies an object. r-value refers to the data
    12 min read
    Const Reference vs Normal Parameter Passing in C++
    In C++, when we call a function we can also pass the parameters to the function in many ways like pass by value, pass by reference, pass by pointer, and by const reference. Each parameter-passing technique has its own advantages and disadvantages. In this article, we will learn the difference betwee
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences