Thread: binary trees..compiler error?

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    58

    binary trees..compiler error?

    Im trying to write a binary tree library and this is the only function giving me trouble(so far..)


    when i compile, it says:

    bintree.c:32: error: void value not ignored as it ought to be
    bintree.c:34: error: void value not ignored as it ought to be


    Code:
    typedef struct _bnode{
        char *name;
        struct _node family_node;
        struct _bnode *left;
        struct _bnode *right;
    } BNODE;
    
    
    void tree_insert(char *key, void *value, void *tree){
        BNODE *root = (BNODE *)tree;
        if(root == 0){
            root = (BNODE *)malloc(sizeof(BNODE));
            root->name = strdup(key);
            root->left = NULL;
            root->right = NULL;
        }else{
            if(strcmp(key, root->name) == -1){
                root->left = tree_insert(key, value, root->left); <-- line 32
            }else if(strcmp(key, root->name) == 1){
                root->right = tree_insert(key, value, root->right); <--line 34
            }else if(strcmp(key, root->name) == 0){
                //do something
    
            }
        }
    }
    will someone please tell me what im doing wrong?

    thankyou

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
    typedef struct _bnode{
        char *name;
        struct _node family_node;
        struct _bnode *left;
        struct _bnode *right;
    } BNODE;
    
    
    void tree_insert(char *key, void *value, void *tree){
        BNODE *root = (BNODE *)tree;
        if(root == 0){
            root = (BNODE *)malloc(sizeof(BNODE));
            root->name = strdup(key);
            root->left = NULL;
            root->right = NULL;
        }else{
            if(strcmp(key, root->name) == -1){
                root->left = tree_insert(key, value, root->left); <-- line 32
            }else if(strcmp(key, root->name) == 1){
                root->right = tree_insert(key, value, root->right); <--line 34
            }else if(strcmp(key, root->name) == 0){
                //do something
    
            }
        }
    }
    You are trying to make something equal to something that has no return. I believe what you want to do is have this function return void * instead.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > void tree_insert
    Returns nothing, nada, zip, zilch

    > root->left = tree_insert(key, value, root->left);
    Expects, something, anything, preferably a struct _bnode *
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    Quote Originally Posted by master5001 View Post
    Code:
    typedef struct _bnode{
        char *name;
        struct _node family_node;
        struct _bnode *left;
        struct _bnode *right;
    } BNODE;
    
    
    void tree_insert(char *key, void *value, void *tree){
        BNODE *root = (BNODE *)tree;
        if(root == 0){
            root = (BNODE *)malloc(sizeof(BNODE));
            root->name = strdup(key);
            root->left = NULL;
            root->right = NULL;
        }else{
            if(strcmp(key, root->name) == -1){
                root->left = tree_insert(key, value, root->left); <-- line 32
            }else if(strcmp(key, root->name) == 1){
                root->right = tree_insert(key, value, root->right); <--line 34
            }else if(strcmp(key, root->name) == 0){
                //do something
    
            }
        }
    }
    You are trying to make something equal to something that has no return. I believe what you want to do is have this function return void * instead.



    so should i return a bnode at the //do somthing? I had a bnode returning there.. and it gave me an error.. soo... where should i return the bnode?

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
    typedef struct _bnode{
        char *name;
        struct _node family_node;
        struct _bnode *left;
        struct _bnode *right;
    } BNODE;
     
     
    BNODE *tree_insert(char *key, void *value, void *tree){
        BNODE *root = (BNODE *)tree;
        if(root == 0){
           root = (BNODE *)malloc(sizeof(BNODE));
           if(root != NULL)
           {
               root->name = strdup(key);
               root->left = NULL;
               root->right = NULL;
           }
        }else{
            if(strcmp(key, root->name) == -1){
                root->left = tree_insert(key, value, root->left); <-- line 32
            }else if(strcmp(key, root->name) == 1){
                root->right = tree_insert(key, value, root->right); <--line 34
            }else if(strcmp(key, root->name) == 0){
                //do something
     
            }
        }
     
        /* Here */
        return root;
    }
    Last edited by master5001; 11-05-2008 at 01:30 PM.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    Quote Originally Posted by master5001 View Post
    Code:
    typedef struct _bnode{
        char *name;
        struct _node family_node;
        struct _bnode *left;
        struct _bnode *right;
    } BNODE;
     
     
    BNODE *tree_insert(char *key, void *value, void *tree){
        BNODE *root = (BNODE *)tree;
        if(root == 0){
           root = (BNODE *)malloc(sizeof(BNODE));
           if(root != NULL)
           {
               root->name = strdup(key);
               root->left = NULL;
               root->right = NULL;
           }
        }else{
            if(strcmp(key, root->name) == -1){
                root->left = tree_insert(key, value, root->left); <-- line 32
            }else if(strcmp(key, root->name) == 1){
                root->right = tree_insert(key, value, root->right); <--line 34
            }else if(strcmp(key, root->name) == 0){
                //do something
     
            }
        }
     
        /* Here */
        return root;
    }

    yay thankyou so much!

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Yeah no problem. The reason for the blue code is because you can at least cope with malloc() returning nothing now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM