Passing pointers by refrence

Hello,
I have tried to create a binary search tree and perform the basic insert and print functions of it. The code for it works all right and is given below.
In all the functions, i pass a pointer by refrence as follows:

1
2
3
4
5

void MakeNewNode(bstNode * & , int);
void Insert (bstNode * &, int);
void Print(bstNode* & );


I use the syntax "type * & pointer_name". Is it the same as "pointer_type ** pointer_name". Is the first style correct? Is it the way to do it in C++11? Is the one with double asterik(pointer_type ** pointer_name) the C way of doing things?

I am only interested in what the pointer 'points to' and not the pointers own address.

Regards,
Saherch


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace std;

class bstNode{
    public:
    int data;
    bstNode * left;
    bstNode * right;
};

void MakeNewNode(bstNode * & , int);
void Insert (bstNode * &, int);
void Print(bstNode* & );

int main(int argc, char *argv[])
{
    bstNode * root = NULL;
    Insert(root, 10);
    Insert(root,5);
    Insert(root,15);
    Insert(root, 2);
    Insert(root,12);

    Print(root);


    return 0;
}

void Print(bstNode* & root )
{
    if (root == NULL)
    {
        return;
    }
    else
    {
        Print(root->left);
        Print(root->right);
        cout<< root->data <<" ,";
    }
}

void Insert (bstNode* & root , int data)
{
    if (root == NULL )
    {
        MakeNewNode(root, data);
    }
    else if (data <= root-> data)
    {
        Insert(root->left, data);
    }
    else if(data > root->data)
    {
        Insert(root->right, data);
    }
}

void MakeNewNode(bstNode* & root , int data)
{
    bstNode * ptr = new bstNode() ;
    ptr->data = data;
    ptr->left = NULL;
    ptr->right = NULL;
    root = ptr;
}
Last edited on
This has nothing to do with C++11. Pointers and references were part of C++ since the beginning.

1) Why not:
bstNode* MakeNewNode(int);

(because it is better to return a value using the return value mechanism, than using a reference to return a value.)

2)
void Insert (bstNode * &, int);

The reference is unnecessary here.

3)
void Print(bstNode* & );

Again, the reference is unnecessary.

Or, perhaps, if you wanted to use references:

bstNode& MakeNewNode(int);
void Insert (bstNode&, int);
void Print(bstNode& );
I am really confused here. I use references because i have to change and update in the main function what the root(pointer) points to. Like initially when the BST is empty, it points to NULL. Then when a new first node is created, root(pointer) should point to it.

Now the root (pointer) would be pointing to root node.

When u suggest that i should use:

void Insert (bstNode&, int);
void Print(bstNode& );

what does it mean?
Because I am passing a pointer to bstNode hence the asterik sign

void Insert (bstNode*, int);
void Print(bstNode* );

So, once again, can you please try to explain how passing a pointer works without the aesterik sign? I apologize if i sound too confused, which BTW i really am :)
You can do, for example:

bstNode* root = NULL;
root = &MakeNewNode(int);

References cannot be NULL, so MakeNewNode hast to return a valid node.

bstNode& MakeNewNode(int data)
{
bstNode& ref = *new bstNode() ;
ptr.data = data;
ptr.left = NULL;
ptr.right = NULL;
return ref;
}

The rererences behave syntactically the same way as values do, but they are implemented by pointers, i.e. the value they are referring to is not copied around, only the reference (pointer) is copied.
Last edited on
void Insert (bstNode&, int);
void Insert (bstNode*, int);

This two declarations are different, but they will essentially do exactly the same thing.

what does it mean?
Because I am passing a pointer to bstNode hence the asterik sign


When you are passing a reference, it's almost the same thing as passing a pointer. It's just that the syntax and capabilities of references are different than those of pointers (like, you can increment a pointer, but the equivalent with references doesn't exist).
Last edited on
I'm oversimplifying, but here is another perspective on references:

References are for people who are tired of writing the dereferencing operator '*' everywhere in the code.
closed account (48T7M4Gy)
http://www.geeksforgeeks.org/when-do-we-pass-arguments-by-reference-or-pointer/
http://www.cplusplus.com/articles/z6vU7k9E/
Last edited on
Topic archived. No new replies allowed.