Skip to content

Warning Assignment From Incompatible Pointer Type Array

  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

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

    --
    Mats


  3. Registered User
    Join Date
    Sep 2007
    Posts
    30
  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.

    #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. Chinese pâté
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Hum... let me guess...
    while (root == NULL) { printTree(root->left); printf(" &#37;d", root->key); printTree(root->right); }
    ...maybe you wanted to write this....

    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. 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. 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?

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

  8. Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    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
    If I shifted the code around a little so that it was like below would that fix it?

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

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

    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. 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?


This is the mail archive of the mailing list for the GCC project.


Re: Avoiding "assignment from incompatible pointer type" warning

  • From: "Joshua Nye" <josh at boxcarmedia dot com>
  • To: "Steve Dondley" <s at dondley dot com>, <gcc-help at gcc dot gnu dot org>
  • Date: Sun, 13 Oct 2002 12:28:10 -0400
  • Subject: Re: Avoiding "assignment from incompatible pointer type" warning
  • References: <FAECKOGIHAIBBPKBOKGNEEDNDOAA.s@dondley.com>

Hi Steve, What you're looking for is a cast. i.e.: int *p; float g = 3.141592653589793238; p = (int *)&g; --josh ----- Original Message ----- From: "Steve Dondley" <s@dondley.com> To: <gcc-help@gcc.gnu.org> Sent: Sunday, October 13, 2002 12:13 PM Subject: Avoiding "assignment from incompatible pointer type" warning > Hi, > > I've written a simple program that print out each bit of a floating point > variable so I can learn how floating point numbers are represented in > memory. The program contains the following statements: > > int *p; > float g = 2.0625; > p = &g; > > The third statement above generates an "assignment from incompatible pointer > type" warning for obvious reasons. Other than this, the program compiles > fine and works. > > My question is: Is there a way to properly assign a pointer of one type to > a variable of different type so that the warning is suppressed? > > Thanks again. > >