Pointer to pointer doesn't work in tree copying algorithm

aditya_medhe

I'm trying to write a tree copy function in C:

void tree_copy(treenode *source, treenode **dest)
{
    treenode *newtree;      //the new tree will be created using this as root   
    treenode **bkup = &newtree;         //pointer to pointer, to keep backup of head of newtree

    /*
    Code to create new tree
    Traverses using *newtree
    And leaves *newtree pointing at last node created
    But *bkup still contains the address of the head node, right?
    */

    *dest = *bkup;  //assign the new tree created to the parameter
}

//I'm trying to invoke it like this:

int main(void)
{
    treenode *t1, *t2;

    create_tree(&t1);   //tested, already written function to create a tree. No bugs.
    tree_copy(t1, &t2);

    preorder(t2);       //tested, already written function for preorder traversal.
}

The node supposed to contain the newly created tree's root (t2) still remains NULL after this operation. Why is this happening? What is wrong in my logic in backing up the starting node of new tree?

Any help will be greatly appreciated.

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

bkup contains the address of the newtree variable. The newtree variable will contain the address of a treenode.

So bkup doesn't contain a copy of the pointer stored in newtree, it contains the address of the newtree variable. Storing that is not really helpful, since it won't change anyway. The newtree variable will stay in the same place, no matter what content you assign to it.

If you want to save a copy of the initial value of newtree, you have to copy that to a newtree* variable once it got initialized:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Copies of a pointer into a pointer doesn't work in C

From Dev

Why does adding to a pointer with += work, but pointer + 1 doesn't?

From Dev

Checking NULL Pointer In C Doesn't Work

From Dev

Pointer to struct doesn't work properly?

From Dev

Copying a pointer

From Dev

Copying for struct pointer to pointer

From Dev

C++ My String class: Pointer doesn't work

From Dev

hex << setw() << setfill() doesn't work with pointer value output

From Dev

Emplace a pointer to a multimap of shared_ptr's doesn't work

From Dev

Why does derefenced pointer increment/decrement doesn't work?

From Dev

C printing double type pointer value doesn't work

From Dev

why swapping value using pointer doesn't work?

From Dev

Anchor mouse pointer doesn´t work with CSS and bootstrap

From Dev

Malloc after moving the pointer doesn't work in C?

From Dev

c: when using a pointer as input in a function incrementing the pointers value by using *pointer++ doesn't work

From Dev

Copying a pointer and then calling delete

From Dev

Copying a pointer and then calling delete

From Dev

image of binary tree and pointer to pointer

From Dev

Doesn't a 2D array decay to pointer to pointer

From Dev

Doesn't a 2D array decay to pointer to pointer

From Dev

Copying a pointer of a pointer (matrix) values in C

From Dev

Copying to clipboard doesn't work javascript

From Dev

c++ when copying inner function pointer to object to function passed pointer the value don't change

From Dev

Why doesn't -fpie work in bare-metal codes and cause wild pointer?

From Dev

Restrict pointer variable to class and sub-classes: Protected doesn't quite work

From Dev

Sorting algorithm doesn't work

From Dev

CSS cursor:pointer; won't work

From Dev

Dereferencing a pointer to a frame that doesn't exist anymore

From Dev

Method pointer template doesn't compile

Related Related

  1. 1

    Copies of a pointer into a pointer doesn't work in C

  2. 2

    Why does adding to a pointer with += work, but pointer + 1 doesn't?

  3. 3

    Checking NULL Pointer In C Doesn't Work

  4. 4

    Pointer to struct doesn't work properly?

  5. 5

    Copying a pointer

  6. 6

    Copying for struct pointer to pointer

  7. 7

    C++ My String class: Pointer doesn't work

  8. 8

    hex << setw() << setfill() doesn't work with pointer value output

  9. 9

    Emplace a pointer to a multimap of shared_ptr's doesn't work

  10. 10

    Why does derefenced pointer increment/decrement doesn't work?

  11. 11

    C printing double type pointer value doesn't work

  12. 12

    why swapping value using pointer doesn't work?

  13. 13

    Anchor mouse pointer doesn´t work with CSS and bootstrap

  14. 14

    Malloc after moving the pointer doesn't work in C?

  15. 15

    c: when using a pointer as input in a function incrementing the pointers value by using *pointer++ doesn't work

  16. 16

    Copying a pointer and then calling delete

  17. 17

    Copying a pointer and then calling delete

  18. 18

    image of binary tree and pointer to pointer

  19. 19

    Doesn't a 2D array decay to pointer to pointer

  20. 20

    Doesn't a 2D array decay to pointer to pointer

  21. 21

    Copying a pointer of a pointer (matrix) values in C

  22. 22

    Copying to clipboard doesn't work javascript

  23. 23

    c++ when copying inner function pointer to object to function passed pointer the value don't change

  24. 24

    Why doesn't -fpie work in bare-metal codes and cause wild pointer?

  25. 25

    Restrict pointer variable to class and sub-classes: Protected doesn't quite work

  26. 26

    Sorting algorithm doesn't work

  27. 27

    CSS cursor:pointer; won't work

  28. 28

    Dereferencing a pointer to a frame that doesn't exist anymore

  29. 29

    Method pointer template doesn't compile

HotTag

Archive