gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 08:15:55 30 4
gpt4 key购买 nike

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 的新手)很糟糕,所以这可能是我忽略的一个非常简单的错误。抱歉,如果这个问题之前已经回答过。

最佳答案

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

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

关于c - 结构和指针有问题(不兼容的指针类型),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35832213/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com