Search code examples
cpointersbinary-search-treedereferenceincomplete-type

C - Dereferencing pointer to incomplete type - insert string into Binary Search Tree


I get a dereferencing pointer to incomplete type on line 58: rootNode->_left = NULL. Any ideas?

Also there is a lot of code commented out to single out this error but I have another question about the format of this ADT: The usual Binary Search Tree structures out there that just have a Node class and all the BST functions such as insert take and return a node. Here however I have to use a separate Tree structure that has a root which is another structure TNode. This has been problematic for me for example in the addStringToTree function it only returns and takes a Tree parameter. So I don't know how to recurse on that in the usual way with Nodes. I created a helper function as my solution but not sure this is ideal.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>

typedef struct TNode {
struct TNode* _left;
struct TNode* _right;
struct TNode* _key;
} TNode;

typedef struct Tree {
TNode* _root;
} Tree;

Tree* makeEmptyTree();
void destroyTree(Tree* root);
Tree* addStringToTree(Tree* t, char* value);
TNode* addStringToTreeHelper(TNode* node, char* value);
bool lookupInTree(Tree* t, char* value);
void traverse(TNode* root);

struct Tree* wordTree;
struct TNode* wordRoot;


int main() {

if(wordTree = makeEmptyTree()) {
    printf("Tree initialized.\n");

///     traverse(wordTree->_root);
    addStringToTree(wordTree, "peter");

    //printf(wordTree->_root->_key);
    //traverse(wordTree->_root);

} else {
    printf("Error initializing tree.\n");
}

return 0;
}

Tree* makeEmptyTree() { 
struct Tree* theTree = malloc(sizeof(struct Tree*));    // allocate memory for Tree
theTree->_root = NULL;
return theTree;
}


Tree* addStringToTree(Tree* t, char* value) {

if(t->_root == NULL) {
    struct Tnode* rootNode = malloc(sizeof(struct TNode*));
    rootNode->_left = NULL;
    //rootNode = (TNode*)malloc(sizeof(struct TNode));
    //strcpy(rootNode->_key, value);
    // rootNode->_left = NULL;
    // rootNode->_right = NULL;
    //printf(rootNode->_key);
} else {
    //addStringToTreeHelper(root, value);
}

return t;
}

TNode* addStringToTreeHelper(TNode* node, char* value) {
// node = malloc(sizeof(TNode));  // What is going on

if(strcmp(value, node->_key) < 0) {
    node->_left = addStringToTreeHelper(node->_left, value);
} else if(strcmp(value, node->_key) > 0) {
    node->_right = addStringToTreeHelper(node->_right, value);
}
return node;
}

void traverse(TNode* root) {
// if(root != NULL) {
//  traverse(root->_left);
//  printf("%s\n", root->_key);
//  traverse(root->_right); 
// } else {
//  printf("Empty Tree\n");
// }
}

2nd Edit

Wow just a silly typo. Thanks guys. Also the _key variable of TNode should be of type char* instead of struct TNode* rolls eyes


Solution

  • struct Tnode* rootNode = malloc(sizeof(struct TNode*));

    Should be

    struct Tnode* rootNode = malloc(sizeof(struct TNode));

    You're only allocating enough memory for a pointer and then pointing to it.

    EDIT:

    That should be TNode* rootNode not Tnode* rootNode.