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?

Recommended Answers

All 3 Replies

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.

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);
}

Ohh I see now very nice. Thanks guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.