c - Pointer to pointer doesn't work in tree copying algorithm -


i'm trying write tree copy function in c:

void tree_copy(treenode *source, treenode **dest) {     treenode *newtree;      //the new tree created using root        treenode **bkup = &newtree;         //pointer pointer, keep backup of head of newtree      /*     code create new tree     traverses using *newtree     , leaves *newtree pointing @ last node created     *bkup still contains address of head node, right?     */      *dest = *bkup;  //assign new tree created parameter }  //i'm trying invoke this:  int main(void) {     treenode *t1, *t2;      create_tree(&t1);   //tested, written function create tree. no bugs.     tree_copy(t1, &t2);      preorder(t2);       //tested, written function preorder traversal. } 

the node supposed contain newly created tree's root (t2) still remains null after operation. why happening? wrong in logic in backing starting node of new tree?

any appreciated.

treenode *newtree; treenode **bkup = &newtree; 

bkup contains address of newtree variable. newtree variable contain address of treenode.

so bkup doesn't contain copy of pointer stored in newtree, contains address of newtree variable. storing not helpful, since won't change anyway. newtree variable stay in same place, no matter content assign it.

if want save copy of initial value of newtree, have copy newtree* variable once got initialized:

treenode *root = newtree; ... *dest = root; 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -