Difference between struct* and struct*&

Having a linked list for example in the form of a struct. Why do we send the head of the linked list to the function parameter in the form of *& and not just * for example.

1
2
3
4
5
6
7
8
9
10
11
12
13
struct node {
int data;
node *next;
}
void foo(node *&p, int data)
{
//code needed to add an element to the end of linked list
}

int main(){
node *head = NULL;
foo(head, 20);
}


Why do we use *& instead of just * ?
Last edited on
Depends on what the actual code in the function is doing, i.e. the part you left out.

head is a pointer to node.
It is passed by reference as a pointer to a node into the function foo. This allows the value of p itself, which is a pointer (address) to be changed.

In the following code, the first function's parameter is passed by the pointer's value.
The second function's parameter is passed by the pointer's reference.
Assigning to a reference affects the outside variable.
Assigning to a pointer does not.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Example program
#include <iostream>

void foo_val(int* a) // pointer to int
{
    a = new int(5);   
}

void foo_ref(int*& a) // reference to pointer to int
{
    a = new int(3);
}

int main()
{
  int n = 42;

  int* p = &n;
  std::cout << *p << std::endl; // 42
  
  foo_val(p);
  std::cout << *p << std::endl; // still 42
  
  foo_ref(p);
  std::cout << *p << std::endl; // new value! 3
}
Last edited on
Why do we use *& instead of just * ?

Wrong emphasis. The * is part of the int*. You should thus ask why do we use & instead of just ?

1
2
void bar( T );
void bar( T & );

One has by value argument. A function creates a local copy from by value argument. Caller's variable won't change.

The other has by reference argument. The function will reference caller's variable and might change it.


What understandably does confuse is that the T happens to be a pointer in your case. The value of the argument points to a third object. A copy of a pointer points to the same object as the original pointer. That object can be modified via all pointers that point to it.

However, if you make one pointer to point to somewhere else, that wont affect where other pointers point to.
in a lot of code, * is sufficient. You only need the & if you plan to change the POINTER that was passed in (not the data under it, the actual memory location).

Pretend for a moment that a * == unsigned int.

doing that, lets examine:
void foo(type *t);
is (pretending)
void foo(unsigned int t);

now, in the second one, if you changed t locally, it does not change the t that was called, right?

then consider

void foo(type *&t);
which in make believe land becomes

void foo(unsigned int &t); //here, if t changes, the passed in value changes!

so why use *&?
something like this:

void foo(int *&t)
{
t = new int[something]; //this function allocates the memory for t.
//without the &, the memory is leaked and the t back in the calling function may be invalid/null/old/wrong
}

and when not to use it:

void foo(int *&t)
{
t[0] = 0; //no point in the & here.

}

Last edited on
Topic archived. No new replies allowed.