Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Both asterisk and ampersand in a parameter

Tags:

I am reading a book about Binary Search Tree and something weird came up. I came across the following declaration in a function parameter.

BinaryNode * & t

What does it mean? Pointer of the address of t?


For context, this is the code where I saw this. The private insert function is a helper function for public insert function, and private insert function looks for the right place to insert using recursion.

class BST
{
public:
   void insert(const Comparable & item)

private:
   BinaryNode *root;
   struct BinaryNode
   {
       Comparable element;
       BinaryNode *left;
       BinaryNode *right;
       BinaryNode(const Comparable & theElement, BinaryNode *lt, BinaryNode *rt) : 
          element(theElement), left(lt), right(rt) {}
   }
   void insert(const Comparable & item, BinaryNode * & t) const;
};
like image 767
Harry Cho Avatar asked Jan 14 '13 07:01

Harry Cho


People also ask

What does * and & mean in C ++?

Put simply. & means the address-of, you will see that in placeholders for functions to modify the parameter variable as in C, parameter variables are passed by value, using the ampersand means to pass by reference. * means the dereference of a pointer variable, meaning to get the value of that pointer variable.

What does * and & indicate in pointer?

The fundamental rules of pointer operators are: The * operator turns a value of type pointer to T into a variable of type T . The & operator turns a variable of type T into a value of type pointer to T .

What is & in C++ function?

The ampersand symbol & is used in C++ as a reference declarator in addition to being the address operator. The meanings are related but not identical. int target; int &rTarg = target; // rTarg is a reference to an integer.

Why do we use & in C?

The ampersand is the address of operator. It returns the memory location of a variable and that's the only way it's used, prefixed to a variable like the engine on a train. The variable doesn't even have to be initialized, just declared.


2 Answers

In your expression BinaryNode * & t)

            BinaryNode*                & t
           -------------              -----
            BinaryNode pointer        t is reference variable  

so t is reference to pointer of BinaryNode class.

Pointer of the address of t?

You are confused ampersand & operator in c++. that give address of an variable. but syntax is different.

ampersand & in front of some of variable like below:

BinaryNode b;
BinaryNode* ptr = &b;

But following way is for reference variable (its simple not pointer):

BinaryNode b;
BinaryNode & t  = b; 

and your is like below:

BinaryNode b;
BinaryNode* ptr = &b;
BinaryNode* &t  = ptr;  
like image 126
Grijesh Chauhan Avatar answered Oct 05 '22 21:10

Grijesh Chauhan


It's a reference to pointer. You can change pointer in this function and it will be changed outside.

like image 37
ForEveR Avatar answered Oct 05 '22 19:10

ForEveR