Binary Tree, makes pointer from integer without a cast

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 21st100
    New Member
    • Sep 2008
    • 2

    Binary Tree, makes pointer from integer without a cast

    Hey Guys

    I am consistently getting this error and cannot seem to find a solution for it...
    I have pasted the relevant code beneath..
    Any help will be appreciated...

    Thanks

    Code:
    typedef struct node NODE;
    
    
    struct node {
        int number;
        int freq;
        NODE *left, *right;
    };

    Code:
        NODE *new_number,*old_number;

    Code:
       if (input < new_number->number) {
                if (new_number->left == NULL) {
                    new_number->left = input;
                    return 1;
                }
            old_number = old_number->left;
            } else {
                if (old_number->right == NULL) {
                    old_number->right = input;
                    return 1;
                }
                old_number = old_number->right;
            }
        }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Originally posted by 21st100
    Code:
       if (input < new_number->number) {
                if (new_number->left == NULL) {
                    new_number->left = input;
                    return 1;
                }
            old_number = old_number->left;
            } else {
                if (old_number->right == NULL) {
                    old_number->right = input;
                    return 1;
                }
                old_number = old_number->right;
            }
        }
    This looks like tree traversal but it isn't clear why you are using 2 pointers new_number and old_number and how the interact.

    Your described problem is these 2 lines
    new_number->left = input;
    old_number->right = input;

    You are assigning a variable of type int to a pointer of type NODE *. This is not going to work, in the long run you will end up with segmentation faults or tree errors. Imagine trying to add an integer value of 0 to the tree.

    You should be creating a new node, assigning the value to the member number and storing a pointer to the new node.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      In lines #3 and #9 you're trying to set the left or right NODE pointer to 'input'
      which is an int; hence the compiler diagnostic. You have to create a new NODE,
      sets its data value to input and make the pointer point to the new node.

      kind regards,

      Jos

      Comment

      • 21st100
        New Member
        • Sep 2008
        • 2

        #4
        Originally posted by JosAH
        In lines #3 and #9 you're trying to set the left or right NODE pointer to 'input'
        which is an int; hence the compiler diagnostic. You have to create a new NODE,
        sets its data value to input and make the pointer point to the new node.

        kind regards,

        Jos
        Hey Jos

        You were Spot on...i cant believe i missed that...
        I have corrected the error and it works like a charm..
        Thanks for the help,

        Cheers

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by 21st100
          Hey Jos

          You were Spot on...i cant believe i missed that...
          I have corrected the error and it works like a charm..
          Thanks for the help,

          Cheers
          You're welcome; the credits need to be split between me and Banfa because
          he was a split second faster than I was; 51% for me, 49% for Banfa ;-)

          kind regards,

          Jos

          Comment

          Working...