Thread: Problem with call by reference

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    19

    Problem with call by reference

    I have two functions, insertNode and makeTree, both of which are supposed to modify the original tree they are being sent, but only makeTree does. Any changes made to the tree I am using in makeTree remain after the function ends, but any changes made to the tree in insertNode do not remain. I cannot figure out what the difference is and why one works, but the other does not. Thanks!

    I am in the process of trying to debug another program, so the program I have now is not supposed to "do" anything.

    Here is my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    typedef struct treenode
    {
            int info;
            treenode * left;
            treenode * right;
    } treenode;
    
    void makeTree(treenode *tree);
    
    void makeTreeNode(treenode *tree, int info);
    
    void printNodes(treenode *tree);
    
    void insertNode(treenode *tree, int val);
    
    main(){
           //create two trees, tree is balanced; tree2 is unbalanced
           treenode *tree;
           tree = (treenode *) malloc(sizeof(*tree));
           //tree = NULL;
           
           printf("Heading to make tree\n");
           makeTree(tree);
           
           insertNode(tree, 7);
           
           printf("\nPrinting Result: ");
           printNodes(tree);
           
           /*
           //create two trees, tree is balanced; tree2 is unbalanced
           treenode *tree2;
           //tree2 = (treenode *) malloc(sizeof(*tree));
           tree2 = NULL;
           
           printf("Heading to make Tree2\n");
           makeTree(tree2);
           
           insertNode(tree2, 4);
           
           printf("\nPrinting Result: ");
           printNodes(tree2);
           */
           getchar();
           return(0);
           }
           
    void insertNode(treenode *tree, int val){
         printf("at insertnode\n");
                         if(tree==NULL){
                                        printf("tree is null\n");
                                        tree = (treenode *) malloc(sizeof(*tree));
                                        tree->info = val;
                                        tree->left = NULL;
                                        tree->right = NULL;
                                        printNodes(tree);
                                        printf("back from printing\n");
                                        }else{
                                              printf("tree is not null\n");
                                              printf("%d\n", tree->info);
                                              if(tree->info==val) return;
                                              printf("going to test ><\n");
                                              if(tree->info>val){
                                                                 printf("treeinfo >\n");
                                                                 insertNode(tree->left, val);
                                                                 }else{
                                                                       printf("treeinfo <\n");
                                                                       insertNode(tree->right, val);
                                                                       }
                                              }
                         return;
                         }
           
    void printNodes(treenode *tree){     
                        if(tree==NULL) return;
                        printNodes(tree->left);
                        printf("%d ", tree->info);
                        printNodes(tree->right);
                        return;
                        }
           
    void makeTree(treenode *tree){
         
         makeTreeNode(tree, 3);
         
         treenode *tree12;
         tree12 = (treenode *) malloc(sizeof(*tree));
         makeTreeNode(tree12, 2);
         
         treenode *tree13;
         tree13 = (treenode *) malloc(sizeof(*tree));
         makeTreeNode(tree13, 1);
         
         treenode *tree14;
         tree14 = (treenode *) malloc(sizeof(*tree));
         makeTreeNode(tree14, 8);
         
         treenode *tree15;
         tree15 = (treenode *) malloc(sizeof(*tree));
         makeTreeNode(tree15, 5);
         
         treenode *tree16;
         tree16 = (treenode *) malloc(sizeof(*tree));
         makeTreeNode(tree16, 4);
         
         /*
         treenode *tree17;
         tree17 = (treenode *) malloc(sizeof(*tree));
         makeTreeNode(tree17, 8);
         */
         
         //put nodes into tree
         tree->left = tree12;
         tree->right = tree14;
         tree12->left = tree13;
         tree14->left = tree15;
         //tree14->right = tree17;
         tree15->left = tree16;
         
         return;
    }
    
    void makeTreeNode(treenode *tree, int info){
         tree->info = info;
         tree->left = NULL;
         tree->right = NULL;
         return;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It's not syntax, it's just that your insertNode is broken. If you keep going until you get to NULL, you've lost the parent node, so you can never hook the child node into the tree anywhere. Instead you need to do something like
    1. If tree is null, create tree.
    2. Otherwise, keep traveling until we would follow a link to NULL, but DO NOT follow that last null link.
    3. Instead of following that null link, malloc a new node and hook it in as the appropriate child (left or right), replacing that null link we would otherwise have followed.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Code:
         treenode *tree13;
         tree13 = (treenode *) malloc(sizeof(*tree));
         makeTreeNode(tree13, 1);
    Don't cast malloc, see the FAQ. What if malloc fails? You'd essentially doing: null->info = info; which is not good! In short, check for NULL after malloc to see that it succeeded. I also don't see any free()'ing of memory?!

    Perhaps makeTreeNode() could also allocate the tree? Or you could make a function called, allocate_tree_node()?
    Last edited by zacs7; 02-28-2008 at 09:08 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Call by reference help!
    By yigster in forum C Programming
    Replies: 1
    Last Post: 05-02-2008, 09:19 AM
  2. Strange/false errors
    By Ganoosh in forum Windows Programming
    Replies: 8
    Last Post: 10-20-2005, 04:54 PM
  3. Student Having Problem With Reference Parameters
    By verd in forum C Programming
    Replies: 1
    Last Post: 04-19-2005, 11:19 PM
  4. Replies: 2
    Last Post: 05-04-2003, 11:55 AM
  5. Pls help me to do this project in C I need source code
    By sureshmenon74 in forum C Programming
    Replies: 4
    Last Post: 10-04-2001, 06:57 AM