### pass a pointer as call by reference

How to pass a pointer as call by reference?

If I have correctly understood what you mean then you can declare a parameter of a function the following way

 ``123456`` ``````void f( int * &x ){} int i = 10; int *p = &i; f( p );``````
EDIT ... vlad from moscow's example is sufficient.
Last edited on
This is the problem
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108`` ``````#include using namespace std; class btree { public: struct node { int data; node *left; node *right; }; node *root; btree(); void insert(node*,int); void inorder(node*); void preorder(node*); void postorder(node*); node* find(node*,int); }; btree::btree() { root=NULL; } btree::node* btree::find(node* temp,int item) { if(temp==NULL) return temp; else if(item>temp->data) { if (temp->right==NULL) return temp; else return find(temp->right,item); } else if(itemdata) { if (temp->left==NULL) return temp; else return find(temp->left,item); } else return temp; } void btree::insert(node *temp,int item) { node* place=find(temp,item); if(place==NULL) { temp=new node; temp->data=item; temp->left=NULL; temp->right=NULL; root=temp; } else if(itemdata) // insert(place->left,item); { place->left=new node; place->left->data=item; place->left->left=NULL; place->left->right=NULL; } else if(item>place->data) //insert(place->right,item); { place->right=new node; place->right->data=item; place->right->left=NULL; place->right->right=NULL; } } void btree::inorder(node* temp) { if(temp==NULL) return; else { inorder(temp->left); cout<data<<" "; inorder(temp->right); } } int main() { btree a; a.insert(a.root,5); a.insert(a.root,1); a.insert(a.root,4); a.insert(a.root,2); a.insert(a.root,7); a.insert(a.root,6); a.insert(a.root,3); a.inorder(a.root); system("pause"); return 0; }``````

The above program compiles and runs well

But, the program below does not run. As far as my understanding goes, it is because `root` is not pointing to the same place as `temp`

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108`` ``````#include using namespace std; class btree { public: struct node { int data; node *left; node *right; }; node *root; btree(); void insert(node*,int); void inorder(node*); void preorder(node*); void postorder(node*); node* find(node*,int); }; btree::btree() { root=NULL; } btree::node* btree::find(node* temp,int item) { if(temp==NULL) return temp; else if(item>temp->data) //{ // if (temp->right==NULL) // return temp; /* else*/ return find(temp->right,item); // } else if(itemdata) // { // if (temp->left==NULL) // return temp; /* else */return find(temp->left,item); //} else return temp; } void btree::insert(node *temp,int item) { node* place=find(temp,item); if(place==NULL) { temp=new node; temp->data=item; temp->left=NULL; temp->right=NULL; //root=temp; } else if(itemdata) insert(place->left,item); /*{ place->left=new node; place->left->data=item; place->left->left=NULL; place->left->right=NULL; }*/ else if(item>place->data) insert(place->right,item); /* { place->right=new node; place->right->data=item; place->right->left=NULL; place->right->right=NULL; }*/ } void btree::inorder(node* temp) { if(temp==NULL) return; else { inorder(temp->left); cout<data<<" "; inorder(temp->right); } } int main() { btree a; a.insert(a.root,5); a.insert(a.root,1); a.insert(a.root,4); a.insert(a.root,2); a.insert(a.root,7); a.insert(a.root,6); a.insert(a.root,3); a.inorder(a.root); system("pause"); return 0; }``````
Thanks.. but after applying your suggestions, I have the following program which compiles
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253`` ``````btree::node* btree::find(node *&temp,int item) { if(temp==NULL) return temp; else if(item>temp->data) { if (temp->right==NULL) return temp; else return find(temp->right,item); } else if(itemdata) { if (temp->left==NULL) return temp; else return find(temp->left,item); } else return temp; } void btree::insert(node *&temp,int item) { node* place=find(temp,item); if(place==NULL) { temp=new node; temp->data=item; temp->left=NULL; temp->right=NULL; //root=temp; } else if(itemdata) //insert(place->left,item); { place->left=new node; place->left->data=item; place->left->left=NULL; place->left->right=NULL; } else if(item>place->data) // insert(place->right,item); { place->right=new node; place->right->data=item; place->right->left=NULL; place->right->right=NULL; } } ``````

Here `root=temp; ` did not have to be used inside `insert` function. However, the code below is still non functional
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152`` ``````btree::node* btree::find(node*& temp,int item) { if(temp==NULL) return temp; else if(item>temp->data) //{ // if (temp->right==NULL) // return temp; /* else*/ return find(temp->right,item); // } else if(itemdata) // { // if (temp->left==NULL) // return temp; /* else */return find(temp->left,item); //} else return temp; } void btree::insert(node *&temp,int item) { node* place=find(temp,item); if(place==NULL) { temp=new node; temp->data=item; temp->left=NULL; temp->right=NULL; //root=temp; } else if(itemdata) insert(place->left,item); /*{ place->left=new node; place->left->data=item; place->left->left=NULL; place->left->right=NULL; }*/ else if(item>place->data) insert(place->right,item); /* { place->right=new node; place->right->data=item; place->right->left=NULL; place->right->right=NULL; }*/ }``````
What exactly is the problem and where is it? What do you mean by non functional?

In your insert, when `place == NULL` you have found your 'insertion' spot correct? Don't you want `place` to be the `new node` and not your `temp`

Maybe what you need is to have the return type be reference to pointer?

[b]Edit:[/b] Now I see the problem from your other post...
Last edited on
Topic archived. No new replies allowed.