Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incompatible type on assignment, from BSTNode* to BSTNode*

Tags:

c

struct

I am trying to create a binary search tree. I am working on the insert function, but i am getting several incompatible types warnings.

warning C4133: '=' : incompatible types - from 'BSTNode *' to 'BSTNode *'

I am getting these warnings on lines 22, 25, 36, and 36 of my code. I am also getting the warning warning C4133: 'function' : incompatible types - from 'BSTNode *' to 'BSTNode *' on my two recursive calls. I marked the errors in the code below with comments. These are the same type, so I cannot figure out what is causing these warnings.

//BST.c

#include "BST.h"
#include <stdio.h>
#include <stdlib.h>


void insert(BSTNode* top_of_bst,BSTNode* node){


   //no items in bst, add it to the top
   if (top_of_bst == NULL){
      top_of_bst->node_value = node->node_value;
      top_of_bst->left = NULL;
      top_of_bst->right = NULL;
      top_of_bst->parent = NULL;
      return;
   }

  //if the value is smaller check the left child
  if (top_of_bst->node_value >= node->node_value){
      if (top_of_bst->left == NULL){
         node->parent = top_of_bst;  //HERE IS AN ERROR
         node->right = NULL;
         node->left = NULL;
         top_of_bst->left = node;   //HERE IS AN ERROR
         return;
      }
      //if the left child exists, recurse left
      else
         insert(top_of_bst->left, node);  //HERE IS AN ERROR
   }
   //if the value is bigger check the right child
    else{
       if (top_of_bst->right == NULL){
          top_of_bst->right = node; //HERE IS AN ERROR
          node->parent = top_of_bst;   //HERE IS AN ERROR
          node->left = NULL;
          node->right = NULL;
          return;
       }
       //if the child exists, recurse right
       else
          insert(top_of_bst->right, node);   //HERE IS AN ERROR
    }
}

And this is my BstNode header file

#ifndef BSTNODE_H
#define BSTNODE_H

typedef struct BSTNODE{
   struct BSTNode* parent;
   struct BSTNode* left;
   struct BSTNode* right;
   int node_value;
}BSTNode;

#endif
like image 819
Nathan Brinda Avatar asked Apr 26 '26 10:04

Nathan Brinda


2 Answers

In your header file

struct BSTNode* parent;
struct BSTNode* left;
struct BSTNode* right;

should be

struct BSTNODE* parent;
struct BSTNODE* left;
struct BSTNODE* right;

because, while defining the members, BSTNode is not known.

Otherwise, you can also have the typedef struct BSTNODE BSTNode; before the structure definition and use like

BSTNode* parent;
BSTNode* left;
BSTNode* right;
like image 173
Sourav Ghosh Avatar answered Apr 28 '26 23:04

Sourav Ghosh


struct should be like below. You cannot reference typedef alias before the definition is complete.

typedef struct BSTNODE{
   struct BSTNODE* parent;
   struct BSTNODE* left;
   struct BSTNODE* right;
   int node_value;
}BSTNode;
like image 40
Abhijit Annaldas Avatar answered Apr 28 '26 23:04

Abhijit Annaldas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!