Click here to Skip to main content
11,571,948 members (74,493 online)
Click here to Skip to main content

Pointer to Pointer and Reference to Pointer

, 29 Apr 2009 CPOL 490.4K 93
Rate this:
Please Sign up or sign in to vote.
Explains the reason behind using pointer-to-pointer and reference-to-pointer to modify a pointer passed to a function.

Contents

Introduction
Why We Need Them?
Syntax of Pointer to Pointer
Syntax of Reference to Pointer
Syntax of tracking reference to a handle(C++/CLI)(new)
Preference of one over the other?
Do not Mistake Pointer to Pointer arguments
Reference to Pointer type (RTTI)
What Are Other Alternatives?
Conclusion
History

Introduction

This article explains the reason behind using pointer-to-pointer and reference-to-pointer to modify a pointer passed to a function, so as to understand their usage better. For brevity, I use the terms, ptr-to-ptr and ref-to-ptr to represent them respectively. In this article, I'm not going to discuss how to use ptr-to-ptr as a 2 dimensional array or array of pointers. Please note we can use ptr-to-ptr in both C and C++ but we can use ref-to-ptr only in C++.

Why We Need Them?

When we use "pass by pointer" to pass a pointer to a function, only a copy of the pointer is passed to the function. We can say "pass by pointer" is passing a pointer by value. In most cases, this does not present a problem. But 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, that is, it still points to the old variable. The code below demonstrates this behavior.

//global variable
int g_One=1;
//function prototype

void func(int* pInt);

int main()
{
  int nvar=2;
  int* pvar=&nvar;
  func(pvar);
  std::cout<<*pvar<<std::endl;//Will still show 2

  return 0;
}

void func(int* pInt)
{
  pInt=&g_One;
}

Syntax of Pointer to Pointer

This is how you called the function with ptr-to-ptr parameter.

//function prototype
void func(int** ppInt);

int main()
{
  int nvar=2;
  int* pvar=&nvar;
  func(&pvar);
  ....
  return 0;
}

Let us see how it to modify the ptr-to-ptr in the function.

void func(int** ppInt)
{
  //Modify the pointer, ppInt points to
  *ppInt=&g_One;

  //You can also allocate memory, depending on your requirements
  *ppInt=new int;

  //Modify the variable, *ppInt points to
  **ppInt=3;
}

Let me surmarise what all those dereferencing are,

  • ppInt is the ptr-to-ptr. We will never modify this because if we do, we'll lose our grip on the address of the pointer it is pointing to.

  • *ppInt is the pointed pointer. If we modify this, we are modifying the contents of the pointed pointer, which is an address and in the above example, pvar. In other words, we are effectively modifying what pvar points to.

  • **ppInt is the dereferenced twice variable which is what pvar points to.

Syntax of Reference to Pointer

Now let us look at how you call the function with ref-to-ptr parameter

//function prototype
void func(int*& rpInt);

int main()
{
  int nvar=2;
  int* pvar=&nvar;
  func(pvar);
  ....
  return 0;
}

Let us see how it to modify the ref-to-ptr in the function.

void func(int*& rpInt)
{
  //Modify what rpInt and pvar is pointing to, to g_One
  rpInt=&g_One;

  //You can also allocate memory, depending on your requirements
  rpInt=new int;

  //Modify the variable rpInt points to
  *rpInt=3;
}

You may wonder whether, in the above func(), the parameter rpInt is pointer to reference. Just take my word for it that it is called ref-to-ptr and it is ref-to-ptr.

Let me once again summarize what all those dereferencing are,

  • rpInt is the reference for the pointer, pvar in the above example.

  • *rpInt dereferences what pvar point to, so you get the variable the pointer, pvar is pointing to.

Syntax of Tracking Reference to a Handle(C++/CLI)

Let us see how to modify a handle in C++/CLI function using the "tracking reference to a handle" parameter. This C++/CLI handle has nothing to do with Win32 HANDLEs and this handle is a reference to a managed object on the CLI heap. I use an object this time, instead of a Plain Old Data(POD)/primitive data/value type because you can only change a reference to an object.

//function prototype
void func(ClassA^% thObj);

int main()
{
  ClassA^ obj = gcnew ClassA;
  ClassA^ obj2=&obj;
  func(obj2);
  ....
  return 0;
}
void func(ClassA^% thObj)
{
  //Modify what obj2 is referencing, to g_obj
  thObj=g_obj; // g_obj is a global object.
  //You can instantiate a new class
  thObj=gcnew ClassA();
  //Modify the variable thObj is referencing, through a its member function, SetInt().
  thObj->SetInt(3);
}
  • thObj is the tracking reference for the handle, obj2 in the above example.

Preference of one over the other?

Now we have seen the syntax of ptr-to-ptr and ref-to-ptr. Are there any advantages of one over the other? I am afraid, no. The usage of one of both, for some programmers are just personal preferences. Some who use ref-to-ptr say the syntax is "cleaner" while some who use ptr-to-ptr, say ptr-to-ptr syntax makes it clearer to those reading what you are doing.

Do not Mistake Pointer to Pointer Arguments

Do not mistake every ptr-to-ptr arguments as purely ptr-to-ptr. An example would be when some write int main(int argc, char *argv[]) as int main(int argc, char **argv) where **argv is actually an array of pointers. Be sure to check out the library documentation first!

Reference to Pointer type (RTTI)

You cannot use RTTI to find out the type of ref-to-ptr. As typeid() does not support reference types.

void test(int*& rpInt)
{
  std::cout << "type of *&rpInt: " << typeid(rpInt).name() 
    << std::endl;//will show int *

}

What are other alternatives?

If you find that the ptr-to-ptr and ref-to-ptr syntax are rather hard to understand, you can just use the "return the pointer" method.

ClassA* func()
{
  ClassA* p = new ClassA();
  // do my things with p
  // ...
  return p;
}

Conclusion

You may ask if you would ever use ptr-to-ptr and ref-to-ptr in your projects and if it is necessary to know about them. Well, as developers, we use libraries and technologies developed by others. One example would be COM uses ptr-to-ptr to return an interface pointer using CoCreateInstance() and IUnknown::QueryInterface(). Up to some point in your developer career, you are definitely going to come across them. It is good to know them.

History

  • 2009/04/29 Updated the explanations and added tracking reference to a handle in C++/CLI section.
  • 2003/09/01 First release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Shao Voon Wong
Software Developer
Singapore Singapore
IT Certifications

  • IT Infrastructure Library Foundational (ITIL v3)
  • Scrum Alliance Certified Scrum Master (CSM)
  • EC-Council Certified Secure Programmer (ECSP) .NET
  • EC-Council Certified Ethical Hacker (CEH)
  • EC-Council Certified Security Analyst (ECSA)

You may also be interested in...

Comments and Discussions

 
QuestionWhat does "void func(ClassA^% thObj)" mean? Pin
Stan Huang22-Mar-15 17:47
professionalStan Huang22-Mar-15 17:47 
AnswerRe: What does "void func(ClassA^% thObj)" mean? Pin
Shao Voon Wong22-Mar-15 18:06
professionalShao Voon Wong22-Mar-15 18:06 
GeneralRe: What does "void func(ClassA^% thObj)" mean? Pin
Stan Huang22-Mar-15 18:24
professionalStan Huang22-Mar-15 18:24 
GeneralRe: What does "void func(ClassA^% thObj)" mean? Pin
Shao Voon Wong22-Mar-15 20:50
professionalShao Voon Wong22-Mar-15 20:50 
QuestionUpdating a Pointer to a Pointer Pin
David A. Gray17-Nov-14 15:06
memberDavid A. Gray17-Nov-14 15:06 
GeneralNyc article... Pin
Atul Khanduri25-Oct-13 8:39
memberAtul Khanduri25-Oct-13 8:39 
GeneralMy vote of 5 Pin
Arturo Prado9-Jul-13 8:53
memberArturo Prado9-Jul-13 8:53 
Questionreturning pointer? Pin
Member 1006888021-May-13 16:41
memberMember 1006888021-May-13 16:41 
AnswerRe: returning pointer? Pin
Wong Shao Voon22-May-13 17:14
memberWong Shao Voon22-May-13 17:14 
GeneralRe: returning pointer? Pin
Member 1006888022-May-13 18:08
memberMember 1006888022-May-13 18:08 
QuestionReturn the pointer method Pin
Member 98347544-Mar-13 7:44
memberMember 98347544-Mar-13 7:44 
AnswerRe: Return the pointer method Pin
Wong Shao Voon4-Mar-13 20:55
memberWong Shao Voon4-Mar-13 20:55 
GeneralRe: Return the pointer method Pin
Member 98347545-Mar-13 0:09
memberMember 98347545-Mar-13 0:09 
GeneralMy vote of 5 Pin
Morph King22-Sep-12 3:55
memberMorph King22-Sep-12 3:55 
GeneralMy vote of 5 Pin
Anitesh Kumar24-May-12 20:29
memberAnitesh Kumar24-May-12 20:29 
GeneralMy vote of 5 Pin
pradhan cn10-Sep-11 13:06
memberpradhan cn10-Sep-11 13:06 
GeneralMy vote of 5 Pin
Member 724777616-Aug-10 0:23
memberMember 724777616-Aug-10 0:23 
Generalref-to-ptr problem Pin
danhass3-Mar-10 10:36
memberdanhass3-Mar-10 10:36 
Generalpass-by-pointer Pin
zired18-Dec-09 2:56
memberzired18-Dec-09 2:56 
I think in your example you have maid a crucial mistacke and wrong conclusions!!!

// A successful attempt to modify caller arguments
#include <iostream>
using std::cout;
using std::endl;
//global variable
int g_One=1;
//function prototype

void func(int* pInt);

int main()
{
int nvar=2;
int* pvar=&nvar;
func(pvar);
std::cout<<*pvar<<std::endl;//Will show 1

return 0;
}

void func(int* pInt)
{
*pInt=g_One;
}
GeneralRe: pass-by-pointer Pin
Shao Voon Wong2-Feb-10 21:39
memberShao Voon Wong2-Feb-10 21:39 
Generalpass-by-pointer Pin
zired18-Dec-09 2:55
memberzired18-Dec-09 2:55 
Questionsome questions about "reference to pointer" Pin
lhyblue18-Sep-09 3:10
memberlhyblue18-Sep-09 3:10 
AnswerRe: some questions about "reference to pointer" Pin
Shao Voon Wong28-Sep-09 15:13
memberShao Voon Wong28-Sep-09 15:13 
GeneralMy vote of 2 Pin
Country Man4-May-09 22:22
memberCountry Man4-May-09 22:22 
GeneralRe: My vote of 2 Pin
Shao Voon Wong28-Sep-09 15:20
memberShao Voon Wong28-Sep-09 15:20 
Generalwhy not using memcpy Pin
auralius18-Jan-09 13:42
memberauralius18-Jan-09 13:42 
GeneralRe: why not using memcpy Pin
Wong Shao Voon19-Jan-09 14:17
memberWong Shao Voon19-Jan-09 14:17 
GeneralRe: why not using memcpy Pin
auralius22-Jan-09 19:03
memberauralius22-Jan-09 19:03 
GeneralRe: why not using memcpy Pin
Wong Shao Voon27-Jan-09 16:49
memberWong Shao Voon27-Jan-09 16:49 
GeneralReference to a pointer and typing. Pin
David G Hunt22-Sep-08 11:48
memberDavid G Hunt22-Sep-08 11:48 
Generalvery nice article, Pin
morten4129-Oct-07 3:59
membermorten4129-Oct-07 3:59 
GeneralRe: very nice article, Pin
Wong Shao Voon29-Oct-07 17:24
memberWong Shao Voon29-Oct-07 17:24 
GeneralRe: very nice article, Pin
morten4130-Oct-07 1:14
membermorten4130-Oct-07 1:14 
QuestionThanks for it, but how to get it to work in .NET 2005 Pin
MausOnMars15-Oct-07 23:30
memberMausOnMars15-Oct-07 23:30 
AnswerRe: Thanks for it, but how to get it to work in .NET 2005 Pin
Wong Shao Voon15-Oct-07 23:40
memberWong Shao Voon15-Oct-07 23:40 
GeneralRe: Thanks for it, but how to get it to work in .NET 2005 Pin
MausOnMars15-Oct-07 23:57
memberMausOnMars15-Oct-07 23:57 
GeneralRe: Thanks for it, but how to get it to work in .NET 2005 Pin
Wong Shao Voon16-Oct-07 3:41
memberWong Shao Voon16-Oct-07 3:41 
Generalc++ question Pin
EasyPro1-Oct-05 15:31
sussEasyPro1-Oct-05 15:31 
GeneralRe: c++ question Pin
Christian Graus1-Oct-05 19:32
memberChristian Graus1-Oct-05 19:32 
GeneralRe: c++ question Pin
stikves29-Apr-09 11:17
memberstikves29-Apr-09 11:17 
Generalwrite a program which displays its own source code as out put Pin
Anonymous12-Jul-05 11:24
sussAnonymous12-Jul-05 11:24 
GeneralRe: write a program which displays its own source code as out put Pin
Christian Graus12-Jul-05 14:36
memberChristian Graus12-Jul-05 14:36 
GeneralThanks Pin
Anonymous26-Mar-05 3:42
sussAnonymous26-Mar-05 3:42 
QuestionWhen to use ptr-to-ptr and ref-to-ptr? Pin
CBasicNet9-Sep-03 15:22
memberCBasicNet9-Sep-03 15:22 
GeneralAlternative suggestion Pin
hain9-Sep-03 4:26
memberhain9-Sep-03 4:26 
GeneralRe: Alternative suggestion Pin
CBasicNet9-Sep-03 15:17
memberCBasicNet9-Sep-03 15:17 
GeneralReferences Pin
Ralph Walden2-Sep-03 7:14
memberRalph Walden2-Sep-03 7:14 
GeneralRe: References Pin
Forogar4-Sep-03 2:29
memberForogar4-Sep-03 2:29 
GeneralRe: References Pin
Brad Sokol9-Sep-03 5:02
memberBrad Sokol9-Sep-03 5:02 
GeneralRe: References Pin
f211-Oct-04 7:12
memberf211-Oct-04 7:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150624.2 | Last Updated 29 Apr 2009
Article Copyright 2003 by Shao Voon Wong
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid