Reputation: 121
what's the difference in
[ insert_node(&(tmp->left),value);] VS [ tmp=tmp->right; insert_node(&(tmp),value);]
void insert_node(struct btree **bt,int value)
{
btree *tmp= *bt, *r ;
if(*bt==NULL)// first node
{
(*bt)=(struct btree *)malloc(sizeof(btree));
(*bt)->data=value;
(*bt)->left=NULL;
(*bt)->right=NULL;
}
else
{
if(value > tmp->data)
insert_node(&(tmp->right),value);
else
insert_node(&(tmp->left),value);
}
#if 0 //start
OR /** COMMENT START
earlier I had below piece of code but not working
can any please explain what's the difference in
insert_node(&(tmp->left),value); VS [ tmp=tmp->right; insert_node(&(tmp),value);]
COMMENT END **/
else
{
if(value > tmp->data)
tmp=tmp->right;
else
tmp=tmp->left ;
insert_node(&tmp,value);
}
#endif //end
}
Upvotes: 0
Views: 439
Reputation: 19443
In the non-working case, you are giving it the address of your tmp
variable, and therefore updating that pointer, which is on your stack and not going to be used for anything.
In the working case, you are giving the address of the actual pointer in your bt node which is what you want.
Upvotes: 1
Reputation: 18239
There is no semantic difference. #2 is storing the next node in a local variable, which will be a register, whereas #1 is storing the node into the register, but it won't be available as a local variable.
If you need to access the value again, use #2.
Upvotes: 0