0

So I thought I had an understanding of pointers and references but I'm not sure whats going on with int*& x. Does this mean a reference to a pointer to an int? also what use does this have?

3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by jmaple
0

Yes, that's what it means. A reference to a pointer is useful for exactly the same reasons that a reference to any other type is useful.

0

It's very similar to int ** -- a pointer to a pointer. That allows a function to change a pointer declared in the calling function. For example: linked list. When the pointer to the head of a linked list is declared in main() the pointer has to be passed by reference to a function that is going to insert a new node either at the head of the linked list or somewhere else within it.

struct link
{
   struct link *next;
   int x;
};

struct link* Insert(struct link*& head)
{
   struct link* newnode = new struct link;
   head = link; // add a new node to the head of the list
}

int main()
{
   struct link* head = 0;
   Insert(head);
}

Edited by Ancient Dragon: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.