结构和指针有问题(指针类型不兼容)

时间:2016-03-06 20:35:42

标签: c pointers struct command-line compiler-errors

struct sentenceTree {
    char *key;
    int duplicate;
    struct Node *left;
    struct Node *right;
};

struct sentenceTree * Insert(struct sentenceTree *node,char *data) //Line 72
{
    if(node==NULL)
    {
        struct sentenceTree *tempNode;
        tempNode = (struct sentenceTree *)malloc(sizeof(struct sentenceTree));
        tempNode -> key = data;
        tempNode -> left = tempNode -> right = NULL;
        tempNode -> duplicate = 0;
        return tempNode;
    }

    if(compareStringsWithCapitals(data, node -> key) == 1)
    {
        node->right = Insert(node->right,data); //Line 86
    }
    else if(compareStringsWithCapitals(data, node -> key) == -1)
    {
        node->left = Insert(node->left,data); //Line 90
    }
    /* Else there is nothing to do as the data is already in the tree. */
    node -> duplicate++;
    return node;
}

当我为我的程序运行make文件时,我的输出中出现了这个错误:

rm      -f      bstsort
cc      bstsort.c       -o      bstsort
bstsort.c: In function ‘Insert’:
bstsort.c:86: warning: passing argument 1 of ‘Insert’ from incompatible pointer type
bstsort.c:72: note: expected ‘struct sentenceTree *’ but argument is of type ‘struct Node *’
bstsort.c:86: warning: assignment from incompatible pointer type
bstsort.c:90: warning: passing argument 1 of ‘Insert’ from incompatible pointer type
bstsort.c:72: note: expected ‘struct sentenceTree *’ but argument is of type ‘struct Node *’
bstsort.c:90: warning: assignment from incompatible pointer type

我已经尝试了我能想到的一切,甚至是Stack Overflow网站上的一些解决方案,但我仍然遇到同样的错误。我对指针和结构很糟糕(一般来说是C的新手)所以这可能是一个我忽略的非常简单的错误。对不起,如果之前已经回答了问题。

1 个答案:

答案 0 :(得分:2)

一个节点应该是一个子树,否则就不能对它进行递归(正如编译器告诉你的那样),所以你的定义应该是这样的:

struct sentenceTree {
    char *key;
    int duplicate;
    struct sentenceTree *left;
    struct sentenceTree *right;
};