-2

While looking at code of binary search tree, I came across this expression,

Node *&t;

Where Node is struct. Why t needs to be pointer and reference at same time?

Regards

Pranit Kothari
  • 9,721
  • 10
  • 61
  • 137
  • 1
    It's a reference to a pointer. Pointers and references read a lot better if you have the * or & next to the type name so it's just considered a single type instead of a type with modifiers. Node*& t; – Ben May 12 '14 at 06:40
  • Why closing, why downvotes, please suggest whats wrong. I understand its duplicate. But I did not found exact word for it. So couldn't find answer. – Pranit Kothari May 12 '14 at 06:58
  • A question closed as a duplicate doesn't necessarily mean you did anything wrong - it's simply to keep the site clean. I can't really explain the downvotes, but perhaps it had something to do with lack of understanding basic syntax (if you want to learn a language, you should start with a good book or course, both of which should explain this in sufficient detail - and yes, I know there can be bad books or courses which don't explain it sufficiently). – Bernhard Barker May 12 '14 at 17:38

1 Answers1

2

This is a reference to a pointer (since pointers to references are illegal in C++). This means, that t represents some variable, which, in turn, is a pointer to Node.

It is hard to judge, what this variable is used for without some code, but the scenario may look like the following:

void MoveNext(Node * & t)
{
    t = t->next;
}

// (...)

Node * current = head;
MoveNext(current);
// Now current points to second item
Community
  • 1
  • 1
Spook
  • 25,318
  • 18
  • 90
  • 167
  • Pointers to references are illegal because they don't make any sense. – Shoe May 12 '14 at 06:45
  • Oh does that mean instead of using Node**, I am using reference to pointer. – Pranit Kothari May 12 '14 at 06:46
  • Yes, both `Node**` and `Node* &` can be used in this case, though the first one requires more error-checking, is less safe and less convenient to use. The main goal is to pass the pointer to a function in such way, that it can be modified. – Spook May 12 '14 at 06:48
  • @Jefffrey Pointers to references are illegal because references don't necessarily have an address. But the reason this is a reference to a pointer isn't because pointers to references are illegal; it is because the reference and pointer "operators" in a type expression have right to left associativity. – James Kanze May 12 '14 at 08:21