Initialization from incompatible pointer type

This is a discussion on Initialization from incompatible pointer type within the C Programming forums, part of the General Programming Boards category; Can anyone help with the below error, I'm not sure where I've gone wrong and why the pointer is wrong. ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    30

    Initialization from incompatible pointer type

    Can anyone help with the below error, I'm not sure where I've gone wrong and why the pointer is wrong.

    tree.c: In function 'freeTree':
    tree.c:85: warning: initialization from incompatible pointer type

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /* Small program to explore using binary trees.
       The program takes an array filled with random numbers
       and inserts the numbers into a binary tree.
       The program then prints the contents of the tree in
       key order. It should print - 
       0 1 2 3 4 5 6 7 8 9
       The program then free's up the tree.
    */ 
    
    struct treeRecord
    {
       int key;
       struct treeRecord *left, *right;
    };
    
    typedef struct treeRecord treeNode;
    
    void insertArrayInTree(treeNode **root, int array[], int size);
    void insertElementInTree(treeNode **root, int number);
    void printTree(treeNode *start);
    void freeTree(treeNode **start);
    treeNode* newNode(int number);
    
    int main()
    {
       int array[] = { 6, 3, 8, 1, 5, 2, 9, 7, 4, 0 }; 
       treeNode *root = NULL;
    
       insertArrayInTree(&root, array, 10);
       printTree(root);
       printf("\n");
       freeTree(&root);
    
       return 0;
    }
    
    void insertArrayInTree(treeNode **root, int array[], int size)
    {
       /* insert elements of array into correct position in binary tree */
    
       int i;
    
       for (i=0; i<size; i++)
       {
          insertElementInTree(root, array[i]);
       }
    }
    
    void insertElementInTree(treeNode **root, int number)
    {
       treeNode *temp;
    
       if (*root == NULL)
       {
          *root = newNode(number);
       }
       else
       {
          temp = *root;
          if (number < temp->key)
             insertElementInTree(&temp->left, number);
          else
             insertElementInTree(&temp->right, number);
       }
    }
    
    void printTree(treeNode *root)
    {
       /* print the contents of the tree in order */
    
       while (root != NULL)
       {
          printTree(root->left);
          printf(" %d", root->key);
          printTree(root->right);
       }
    }
    
    void freeTree(treeNode **root)
    {
       /* free up memory allocated to binary tree */
       treeNode *temp = root;
    
       if (temp != NULL)
       {
          if (temp->left != NULL) freeTree(&temp->left);
          if (temp->right != NULL) freeTree(&temp->right);
          free(temp);
          *root = NULL;
       }  
    }
    
    treeNode* newNode(int number)
    {
       /* dynamicaly allocate memory for a treeNode
          make sure it is initialised correctly
       */
    
       treeNode *temp;
    
       temp = (treeNode *) malloc(sizeof(treeNode));
       if (temp == NULL)
       {
          printf("WARNING - Memory allocation error\n");
          exit(EXIT_FAILURE);
       }
       temp->key = number;
       temp->left = NULL;
    
       return temp;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm only guessing that this is line 85:
    Code:
       treeNode *temp = root;
    Perhaps you want *temp = *root, since root is treeNode **, and thus *root has teh type treeNode *??

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    30
    Thats it. Thanks.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    30
    I've got another error in the code somewhere but I can't figure out where it is.

    At the moment when I run it I'm not getting any output, just a blank line.

    I'm not to sure what the error is though.

    Any help would be appreciated it.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /* Small program to explore using binary trees.
       The program takes an array filled with random numbers
       and inserts the numbers into a binary tree.
       The program then prints the contents of the tree in
       key order. It should print - 
       0 1 2 3 4 5 6 7 8 9
       The program then free's up the tree.
    */ 
    
    struct treeRecord
    {
       int key;
       struct treeRecord *left, *right;
    };
    
    typedef struct treeRecord treeNode;
    
    void insertArrayInTree(treeNode **root, int array[], int size);
    void insertElementInTree(treeNode **root, int number);
    void printTree(treeNode *start);
    void freeTree(treeNode **start);
    treeNode* newNode(int number);
    
    int main()
    {
       int array[] = { 6, 3, 8, 1, 5, 2, 9, 7, 4, 0 }; 
       treeNode *root = NULL;
    
       insertArrayInTree(&root, array, 10);
       printTree(root);
       printf("\n");
       freeTree(&root);
    
       return 0;
    }
    
    void insertArrayInTree(treeNode **root, int array[], int size)
    {
       /* insert elements of array into correct position in binary tree */
    
       int i;
    
       for (i=0; i<size; i++)
       {
          insertElementInTree(root, array[i]);
       }
    }
    
    void insertElementInTree(treeNode **root, int number)
    {
       treeNode *temp;
    
       if (*root == NULL)
       {
          *root = newNode(number);
       }
       else
       {
          temp = *root;
          if (number < temp->key)
             insertElementInTree(&temp->left, number);
          else
             insertElementInTree(&temp->right, number);
       }
    }
    
    void printTree(treeNode *root)
    {
       /* print the contents of the tree in order */
    
       while (root == NULL)
       {
          printTree(root->left);
          printf(" %d", root->key);
          printTree(root->right);
       }
    }
    
    void freeTree(treeNode **root)
    {
       /* free up memory allocated to binary tree */
       treeNode *temp = *root;
    
       if (temp != NULL)
       {
          if (temp->left != NULL) freeTree(&temp->left);
          if (temp->right != NULL) freeTree(&temp->right);
          free(temp);
          *root = NULL;
       }  
    }
    
    treeNode* newNode(int number)
    {
       /* dynamicaly allocate memory for a treeNode
          make sure it is initialised correctly
       */
    
       treeNode *temp;
    
       temp = (treeNode *) malloc(sizeof(treeNode));
       if (temp == NULL)
       {
          printf("WARNING - Memory allocation error\n");
          exit(EXIT_FAILURE);
       }
       temp->key = number;
       temp->left = NULL;
    
       return temp;
    }

  5. #5
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Hum... let me guess...
    Code:
    while (root == NULL)
    {
       printTree(root->left);
       printf(" &#37;d", root->key);
       printTree(root->right);
    }
    ...maybe you wanted to write this....

    Code:
    while (root != NULL)
    {
       printTree(root->left);
       printf(" %d", root->key);
       printTree(root->right);
    }
    Note that you'll have infinte loop since you aren't changing the value of root. (Why a loop on a recursive function like this by the way? Maybe an if statement would be more appropriate). I didn't read the whole code by the way, just took a fast look at it.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    30
    Hmm I changed that from != to == to stop the inifite "0" output. So I guess I would need to think of another way to get around that then...

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    30
    We aren't allowed to change the structure of the code, we can only make it work so I have to use the loop unfortunately.

    If I shifted the code around a little so that it was like below would that fix it?

    Code:
       while (root == NULL)
       {
          printTree(root->left);
          printTree(root->right);
          printf(" %d", root->key);
       }

  8. #8
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    If I shifted the code around a little so that it was like below would that fix it?

    Code:
       while (root == NULL)
       {
          printTree(root->left);
          printTree(root->right);
          printf(" %d", root->key);
       }
    No that won't work. That loop will never run.
    The real problem seems to be that you never initialize node->right in function newNode().
    Kurt

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    30
    So I would need to use something like:

    Code:
    treeNode* newNode(int number)
    {
       /* dynamicaly allocate memory for a treeNode
          make sure it is initialised correctly
       */
    
       treeNode *temp;
    
       temp = (treeNode *) malloc(sizeof(treeNode));
       if (temp == NULL)
       {
          printf("WARNING - Memory allocation error\n");
          exit(EXIT_FAILURE);
       }
       temp->key = number;
       temp->left = NULL;
       temp->right = NULL;
    
       return temp;
    }

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    30
    Ok so that didn't work and I'm really not sure of anywhere else to go... Any other ideas of where it is wrong?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Return from incompatible pointer type
    By aladin in forum C Programming
    Replies: 2
    Last Post: 03-22-2009, 08:58 AM
  3. Replies: 3
    Last Post: 04-15-2008, 02:16 AM
  4. Replies: 5
    Last Post: 08-12-2007, 05:26 PM
  5. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21