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?
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?
|
|||
|
up vote
19
down vote
accepted
|
First, a reference to a pointer is like a reference to any other variable:
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.:
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. |
||||||
|
up vote
5
down vote
|
Pointer to a pointerA 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 ( It would be stored in memory in a similar way to this (I'm purposely simplifying things for this example):
If you wanted to create a "pointer" to this value, it would look like this in memory:
At memory address Let's say you wanted to create a "pointer" to this pointer value. It would look like this:
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 Reference to a pointerA reference can be thought of as a view, or alias, on to another real object. When you create a reference to a pointer called 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 referenceThis 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 parametersWhen 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:
Let's now say that your method wants to allocate memory for a pointer. You could be tempted to do this:
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":
In these bottom 2 examples, the code which is calling |
|||
|
up vote
4
down vote
|
There is no such thing as a pointer to a reference. |
||
|
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. |
||
|
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:
These posts were very helpful in aiding my understanding. |
||
|
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:
Output:
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. |
||
|
up vote
-2
down vote
|
Here is an excellent article regarding this issue on CodeProject. The author, Shao Voon Wong, concludes:
|
||
|
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. |
||||||||
|