21

I have implemented operations on a list, one of them is add, and since i don't want to return anything, i read that i had to use **, and it works, but i saw on another place that it is passed as *&, but i don't know the difference

addNode( node *&head, int value) addNode(node **head, int value)

What is the difference, and which one is better, or do they mean the same? I know the second is pointer to a pointer.

Thanks

bb2
  • 211
  • 1
  • 2
  • 3

3 Answers3

30

The first (**) is a pointer to a pointer and the second (*&) is a reference to a pointer.

A reference and a pointer are conceptually quite similar. But there are some important differences, for example:

  • A reference cannot be NULL (but it can refer to a pointer which points to NULL).
  • You can't modify a reference to refer to something else.
  • You need to dereference a pointer to access the value.

See this related question for more differences:

Community
  • 1
  • 1
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • At least according to the article I found, it is a pointer to a reference and not the other way around. (I wasn't sure about which one it was myself, so I really don't know for sure.) The difference may be very subtle, but it could cause some confusion in rare cases. – Tim Yates Sep 30 '10 at 19:34
  • 3
    You can't have a pointer to a reference (that's another difference between pointers and references by the way, and it's also mentioned on the page I linked to). See also this question: http://stackoverflow.com/questions/1898524/difference-between-pointer-to-a-reference-and-reference-to-a-pointer – Mark Byers Sep 30 '10 at 19:38
  • Good point. I didn't think that through closely enough. I was just trying to think of it in terms of operator binding. – Tim Yates Sep 30 '10 at 19:42
7

With:

addNode( node *&head, int value)

...the type of head is "reference to pointer-to-node".

With:

addNode(node **head, int value)

... the type is "pointer-to-pointer-to-node".

A pointer and a reference are not the same thing. A simple way to think of a reference is as a dereferenced pointer.

You would need different syntax to call both versions:

node* my_node = 0;
addNode(my_node, 0);  // syntax for first version
addNode(&my_node, 0); // syntax for 2nd version

There are semantic differences as well. When you pass a pointer, you can pass NULL. When you pass a reference, you can't. This being a function that takes a ref-to-ptr confuses the matter a little, so let's change the problem a bit:

void make_string(string& str_ref)
{
  str_ref = "my str";
}

void make_string_again(string* str_ptr)
{
  *str_ptr = "my other string";
}

These two finctions do the same thing but one takes a string reference while the other takes a string pointer. If you were to do this:

string str;
make_string(str); // OK
make_string_again(&str); // OK - &str is a pointer to a real string
make_string_again(0); // Not OK - compiles but will crash when function dereferences the null pointer

You can see it becomes difficult (but not impossible) to call make_string with a null pointer. This could help you to implement better functions in the case where you expect that make_string will never be called with an invalid object.

John Dibling
  • 99,718
  • 31
  • 186
  • 324
1

This is the difference between pass by value and pass by reference. Pass by reference, basically, implicitly does the reference and dereference that the double pointer gets you.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173