user3213297
user3213297

Reputation: 63

What does *& mean when used in argument?

I was wondering was *& means. Context:

A function is implemented as follows:

void headInsert( Node*& head, int info )
{
    Node* temp = new Node(info);
    temp->link = head;
    head = temp;
}

WHy not just use Node& ?

Thanks

Upvotes: 0

Views: 84

Answers (2)

Zacharias
Zacharias

Reputation: 586

You may want to look at the specific call, where reference pointers reveal their use:

Node* pHead = somewhere;
headInsert(pHead, info);
// pHead does now point to the newly allocated node, generated inside headInser, 
// by new Node(info), but NOT to 'somewhere'

Let me comment on your example, maybe that makes it more clear:

void headInsert( Node*& head, int info )
{
    Node* temp = new Node(info); // generate a new head, the future head
    temp->link = head; // let the former head be a member/child of the new head
    head = temp; // 'overwrite' the former head pointer outside of the call by the new head
}

Upvotes: 1

Diego
Diego

Reputation: 1819

Node*& means "a reference to a pointer to Node" while Node& means "a reference to Node".

Why not just use Node& ?

Because the headInsert function needs to change what the head is pointing to.

Upvotes: 3

Related Questions