Click here to Skip to main content
16,018,818 members
Articles / Programming Languages / Markdown

Pointer to Pointer and Reference to Pointer

Rate me:
Please Sign up or sign in to vote.
4.65/5 (87 votes)
11 Jan 2018CPOL4 min read 968K   545   138   63
Explains the reason behind using pointer-to-pointer and reference-to-pointer to modify a pointer passed to a function

Contents

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 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, that is, it still points to the old variable. The code below demonstrates this behavior.

C++
int g_n = 42;

void example_ptr()
{
    int n = 23;
    int* pn = &n;

    std::cout << "example_ptr()" << std::endl;

    std::cout << "Before :" << *pn << std::endl; // display 23

    func_ptr(pn);

    std::cout << "After :" << *pn << std::endl; // display 23
}

void func_ptr(int* pp)
{
    pp = &g_n;
}

This is not a problem with C/C++ only. This is exactly the same case with Python. In the example below where a dictionary, instruments, is initialized again in fill_up method. After the method call, instruments is still empty. Because only a copy of instruments is passed to fill_up. You can think of instruments as pointer which is passed by copy. The initializing code is like calling C++ new to instantiate the dictionary. instruments inside fill_up points to new memory while the outer one still points to original memory. Underneath the surface, computer languages makes use of address to load and fetch data. There is no escape from using address underlyingly, even in Python, a language which does not support pointer and address. In Java, references are disguised pointers or indirect pointers, with meta information, which is dereferenced automatically, whose address is updated after garbage collection.

Python
#!/usr/bin/python

def fill_up(instruments):
    instruments = {} # This line caused the problem
    instruments["1"] = 1

instruments = {}
fill_up(instruments)
print "len(instruments):%d" % len(instruments)

Output shows instruments has zero elements after the call.

len(instruments):0

Initialization is removed in the below code:

Python
#!/usr/bin/python

def fill_up(instruments):
    instruments["1"] = 1

instruments = {}
fill_up(instruments)
print "len(instruments):%d" % len(instruments)

Now, output shows instruments length correctly!

len(instruments):1

Syntax of Pointer to Pointer

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

C++
int g_n = 42;

void example_ptr_to_ptr()
{
    int n = 23;
    int* pn = &n;

    std::cout << "example_ptr_to_ptr()" << std::endl;

    std::cout << "Before :" << *pn << std::endl; // display 23

    func_ptr_to_ptr(&pn);

    std::cout << "After :" << *pn << std::endl; // display 42
}

void func_ptr_to_ptr(int** pp)
{
    *pp = &g_n;
}

Syntax of Reference to Pointer

Now let us look at how you call the function with ref-to-ptr parameter. Many C++ developers mistook this type as pointer to reference. C++ types are inferred by reading from right to left!.

C++
int g_n = 42;

void example_ref_to_ptr()
{
    int n = 23;
    int* pn = &n;

    std::cout << "example_ref_to_ptr()" << std::endl;

    std::cout << "Before :" << *pn << std::endl; // display 23

    func_ref_to_ptr(pn);

    std::cout << "After :" << *pn << std::endl; // display 42
}

void func_ref_to_ptr(int*& pp)
{
    pp = &g_n;
}

Syntax of Returning Pointer

Or you can just simply return the pointer.

C++
int g_n = 42;

void example_ret_ptr()
{
    int n = 23;
    int* pn = &n;

    std::cout << "example_ret_ptr()" << std::endl;

    std::cout << "Before :" << *pn << std::endl; // display 23

    pn = func_ret_ptr();

    std::cout << "After :" << *pn << std::endl; // display 42
}

int* func_ret_ptr()
{
    return &g_n;
}

Syntax of Returning Reference

Or you can just simply return the reference.

C++
int g_n = 42;

void example_ret_ref()
{
    int n = 23;
    int* pn = &n;

    std::cout << "example_ret_ref()" << std::endl;

    std::cout << "Before :" << *pn << std::endl; // display 23

    pn = &func_ret_ref();

    std::cout << "After :" << *pn << std::endl; // display 42
}

int& func_ret_ref()
{
    return g_n;
}

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.

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

}

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

  • 2018/01/11: Add links to show C++ types are inferred by reading from right to left, so as to show *& is reference to pointer, not pointer to reference
  • 2017/08/23: Show Python code which has the same problem as C++
  • 2016/10/02: Simpler examples
  • 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)


Written By
Software Developer (Senior)
Singapore Singapore
Shao Voon is from Singapore. His interest lies primarily in computer graphics, software optimization, concurrency, security, and Agile methodologies.

In recent years, he shifted focus to software safety research. His hobby is writing a free C++ DirectX photo slideshow application which can be viewed here.

Comments and Discussions

 
SuggestionReason for using ptr-to-ptr or ref-to-ptr Pin
Stefan_Lang17-Jan-18 22:38
Stefan_Lang17-Jan-18 22:38 
GeneralRe: Reason for using ptr-to-ptr or ref-to-ptr Pin
Shao Voon Wong28-Jan-18 22:05
mvaShao Voon Wong28-Jan-18 22:05 
QuestionBad stile of examples Pin
Сергій Ярошко13-Jan-18 8:24
professionalСергій Ярошко13-Jan-18 8:24 
AnswerRe: Bad stile of examples Pin
Shao Voon Wong13-Jan-18 17:43
mvaShao Voon Wong13-Jan-18 17:43 
GeneralRe: Bad stile of examples Pin
Сергій Ярошко13-Jan-18 21:44
professionalСергій Ярошко13-Jan-18 21:44 
GeneralRe: Bad stile of examples Pin
Shao Voon Wong28-Jan-18 22:18
mvaShao Voon Wong28-Jan-18 22:18 
SuggestionNice Pin
napuzba4-Sep-17 2:56
napuzba4-Sep-17 2:56 
GeneralRe: Nice Pin
Shao Voon Wong6-Sep-17 18:37
mvaShao Voon Wong6-Sep-17 18:37 
GeneralReference concept Pin
napuzba6-Sep-17 22:27
napuzba6-Sep-17 22:27 
PraiseWell done Pin
ITISAG13-Apr-16 13:08
ITISAG13-Apr-16 13:08 
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
mvaShao 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
mvaShao Voon Wong22-Mar-15 20:50 
QuestionUpdating a Pointer to a Pointer Pin
David A. Gray17-Nov-14 15:06
David A. Gray17-Nov-14 15:06 
GeneralNyc article... Pin
Atul Khanduri25-Oct-13 8:39
Atul Khanduri25-Oct-13 8:39 
GeneralMy vote of 5 Pin
Arturo Prado9-Jul-13 8:53
Arturo Prado9-Jul-13 8:53 
Questionreturning pointer? Pin
Member 1006888021-May-13 16:41
Member 1006888021-May-13 16:41 
AnswerRe: returning pointer? Pin
Shao Voon Wong22-May-13 17:14
mvaShao Voon Wong22-May-13 17:14 
GeneralRe: returning pointer? Pin
Member 1006888022-May-13 18:08
Member 1006888022-May-13 18:08 
QuestionReturn the pointer method Pin
Member 98347544-Mar-13 7:44
Member 98347544-Mar-13 7:44 
AnswerRe: Return the pointer method Pin
Shao Voon Wong4-Mar-13 20:55
mvaShao Voon Wong4-Mar-13 20:55 
GeneralRe: Return the pointer method Pin
Member 98347545-Mar-13 0:09
Member 98347545-Mar-13 0:09 
GeneralMy vote of 5 Pin
Morph King22-Sep-12 3:55
Morph King22-Sep-12 3:55 
GeneralMy vote of 5 Pin
Anitesh Kumar24-May-12 20:29
Anitesh Kumar24-May-12 20:29 

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

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