up vote 10 down vote favorite
6

What is the difference between pointer to a reference, reference to a pointer and pointer to a pointer in C++?

Where should one be preferred over the other?

flag

67% accept rate

8 Answers

up vote 19 down vote accepted

First, a reference to a pointer is like a reference to any other variable:

void fun(int*& ref_to_ptr)
{
    ref_to_ptr = 0; // set the "passed" pointer to 0
    // if the pointer is not passed by ref,
    // then only the copy(parameter) you received is set to 0,
    // but the original pointer(outside the function) is not affected.
}

A pointer to reference is illegal in C++, because -unlike a pointer- a reference is just a concept that allows the programmer to make aliases of something else. A pointer is a place in memory that has the address of something else, but a reference is NOT.

Now the last point might not be crystal clear, if you insist on dealing with references as pointers. e.g.:

int x;
int& rx = x; // from now on, rx is just like x.
// Unlike pointers, refs are not real objects in memory.
int* p = &x; // Ok
int* pr = ℞ // OK! but remember that rx is just x!
// i.e. rx is not something that exists alone, it has to refer to something else.
if( p == pr ) // true!
{ ... }

As you can see from the above code, when we use the reference, we are not dealing with something separated from what it refers to. So, the address of a reference is just the address of what it refers to. Thats why there is no such thing called the address of the reference in terms of what you are talking about.

link|flag
The most important point (as you have expressed) is that you can get a pointer to a reference (or a reference to a reference) or anything like that. As the reference is just an alias to another obejct. – Martin York Dec 14 '09 at 2:49
1  
in above comment s/can/can't/ – jk Dec 14 '09 at 9:53
up vote 5 down vote

Pointer to a pointer

A pointer in C++ is just a value which stores a memory location (generally as a 32-bit value).

Let's say you had an user input integer value (78 == 0x4E in hex).

It would be stored in memory in a similar way to this (I'm purposely simplifying things for this example):

Memory address    Value
0x12345678        0x0000004E

If you wanted to create a "pointer" to this value, it would look like this in memory:

Memory address    Value
0x22334455        0x12345678

At memory address 0x22334455 you now have a "pointer" whose value is 0x12345678, or the memory address of where the user input integer value (0x4E) is stored.

Let's say you wanted to create a "pointer" to this pointer value. It would look like this:

Memory address    Value
0x11335577        0x22334455

You now have a new "pointer" value in memory which is storing the memory address of the previously-defined pointer value.

Pointers can be created like this indefinitely - the key is remembering that a pointer is just another value that the compiler interprets as a memory location (and it provides various access semantics such as * and -> which are special to "pointer" types).

Reference to a pointer

A reference can be thought of as a view, or alias, on to another real object. When you create a reference to a pointer called myReference, you are simply defining a new name called myReference which can be used to access the pointer which you have previous defined in memory.

Internally, references are implemented using pointers, but this is beyond the scope of your question.

References have restrictions over other types in C++ - for example, you must always initialize a reference to "refer" to a real object when you create it, while a pointer may point to memory which is invalid, or uninitialised.

Pointer to a reference

This doesn't exist. As stated earlier, a reference is merely an alias to another object. You can't "point" to a reference, because it isn't an object in itself but merely another name for a real object.

Of course, you can have a pointer to the object that a reference is referring to. But now we are back in vanilla pointer territory.

Note about parameters

When you pass a parameter by value to a method or routine, you are essentially passing a "copy" of the object to the method. Any changes you make to the value within the routine will be lost when the routine returns, because the parameter will be treated as a local variable in the context of the routine.

If you want to modify a parameter which is passed in so the client (calling) code can access the change, you must pass the parameter by pointer or by reference.

For example:

void myMethod(int myValue)
{
    // NOTE: This change will be lost to the caller!
    myValue = 5;
}

void myMethod2(int* myValue)
{
    // Correct way of modifying pointer parameter value
    *myValue = 5;
}

void myMethod3(int& myValue)
{
    // Correct way of modifying reference parameter value
    myValue = 5;
}

Let's now say that your method wants to allocate memory for a pointer. You could be tempted to do this:

void myMethod4(int* myValue)
{
    // Warning: You will lose the address of the allocated
    // memory when you return!
    myValue = new int[5];
}

But remember, you are modifying the copy of the pointer value here, not the real pointer value. Since you are wanting to modify the pointer in this routine, and not the value that the pointer "points" to, you need to pass it in as a "pointer to a pointer" or a "reference to a pointer":

void myMethod5(int** myValue)
{
    // Correct way of allocating memory in a method
    // via pointer-to-pointer
    *myValue = new int[5];
}

void myMethod6(int*& myValue)
{
    // Correct way of allocating memory in a method
    // via reference-to-pointer
    myValue = new int[5];
}

In these bottom 2 examples, the code which is calling myMethod5 and myMethod6 will correctly get the memory address of the newly-allocated memory via the myValue parameter pointer or reference.

link|flag
up vote 4 down vote

There is no such thing as a pointer to a reference.

link|flag
up vote 4 down vote

A reference is an abstraction away from pointers. References are a bit harder to screw up, especially for novices, and are a bit more high level.

You don't need references. You can always use pointers. However, sometimes code can be easier to read with them.

A typical beginner example is a linked list. Imagine you have a variable called "list" that contains a pointer to the first one. If you wanted to add something to the head, you'd need to give your add() a double pointer, since it needs to be able to modify "head". However, you can use a reference to a pointer instead. Here, we want to use pointers in the list itself since we'll be mutating them, but the add() function will be clearer if we pass in a reference to the head of the list instead of a double pointer.

They're simply a style choice. If you're working on a larger project, you should go with the style of the project. If not, you can use whatever you feel is preferable. You should, however, be comfortable using all styles if you even hope to be a mildly successful C++ programmer.

It is also worthwhile that you can't have a pointer to a reference. This is because references are really just another name for another variable, which may be in some other scope. Having a pointer to a reference doesn't make sense. What you'd really want is just a pointer to whatever the original data was, no references involved.

link|flag
up vote 0 down vote

I am a beginning C++ programmer. I know what a pointer is and what a reference is. I did not know what the following was:

 *& a_variable

These posts were very helpful in aiding my understanding.

link|flag
up vote -1 down vote

Just try and see for yourself what is each thing holding. The sample program simply prints the value for an int and the addresses of different entities:

#include<stdio.h>

int main(){
  int myInt ;
  int *ptr_to_myInt = &myInt;
  int *ptr_to_myInt_ref = ptr_to_myInt;

  myInt = 42;

  printf("myInt is %d\n",myInt);
  printf("ptr_to_myInt is %x\n",ptr_to_myInt);
  printf("ptr_to_myInt_ref is %x\n",ptr_to_myInt_ref);
  printf("&ptr_to_myInt is %x\n",&ptr_to_myInt);

  return 0;
}

Output:

myInt is 42
ptr_to_myInt is bffff858
ptr_to_myInt_ref is bffff858
&ptr_to_myInt is bffff854

So, the pointer to the int and the pointer to the reference of the int are exactly the same thing. This is obvious from the code, because the pointer to a reference is simply another way of aliasing a pointer (it is saying "hold the following address for me").

Now, the pointer also needs some space in memory, and if you print the reference to this pointer (the last printf statement) it simply indicates the place in memory where the pointer resides.

link|flag
up vote -2 down vote

Here is an excellent article regarding this issue on CodeProject.

The author, Shao Voon Wong, concludes:

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.

link|flag
up vote -7 down vote

Are you really waiting for an answer, or for someone to teach you C++ from scratch ?

I can't find an answer to your question (is it?) other than "well, just read any C++ book to learn what are the differences between pointers and references".

--EDIT

Wake up guys, appending a question mark at the end of a sentence does not make it a question. There is no question here because there is no problem.

The whole subject here is to make us waste our time answering by paraphrasing what is on any C++ introduction.

What will be the next question ? What's the difference between a byte and a char ? What's the difference between an integer and a pointer ?

The problem with this forum is that anyone can ask the first thing that comes to his mind without even trying to figure it, there will always be people ready to lose their time by considering the question... This definitely encourage them in the wrong direction.

link|flag
First of all this is not an answer. SO if you want to write sarcastic comments please use the comment section. Secondly the idea was to know where to use what and this surely comes by experience and no book teaches that!!! – bakore Dec 14 '09 at 1:53
Again the same comment for you..This is the answer section use the comment section for such things – bakore Dec 14 '09 at 18:13
1  
@Aurélien Vallée: Joel himself set the mark for this. The rule is: "no question too basic". And the example that he wrote to emphasize the point is How do I move the turtle in LOGO? . Yes, the question is very basic. So ether answer it or don't, but kindly do not cast aspersions on the author simply because it is a basic question. – dmckee Jun 25 at 22:54

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.