从结构创建 BST 节点时出现编译错误
在我的代码中,有一个二进制树结构定义为:
typedef struct bintreestruct *bintree;
struct bintreestruct
{
double num;
char *s;
bintree l, r;
};
我想将一个节点插入此二进制搜索树中。这是功能:
void insbintree(double i, char *s, bintree *t)
{
if (t == NULL)
{
bintree temp = (struct bintreestruct *)malloc(sizeof(struct bintreestruct));
temp->s = s;
temp->num = i;
temp->l = temp->r = NULL;
return temp;
}
if (strcmp(s, t->s) < 0)
t->l = insert(t->l, s);
else if (strcmp(s, t->s) >= 0)
t->r = insert(t->r, s);
return t;
}
我得到错误错误:'*t'是指指针;您是说使用' - &gt;'? 62 | if(strcmp(s,t-&gt; s)&lt; 0)
我要么不正确地创建新节点,要么访问
中的元素 使用指针的错误方式。不确定如何纠正此错误
In my code, there is a binary tree struct defined as :
typedef struct bintreestruct *bintree;
struct bintreestruct
{
double num;
char *s;
bintree l, r;
};
I wanted to insert a node into this binary search tree. Here is the function:
void insbintree(double i, char *s, bintree *t)
{
if (t == NULL)
{
bintree temp = (struct bintreestruct *)malloc(sizeof(struct bintreestruct));
temp->s = s;
temp->num = i;
temp->l = temp->r = NULL;
return temp;
}
if (strcmp(s, t->s) < 0)
t->l = insert(t->l, s);
else if (strcmp(s, t->s) >= 0)
t->r = insert(t->r, s);
return t;
}
I am getting the error error: ‘*t’ is a pointer; did you mean to use ‘->’? 62 | if (strcmp(s, t->s) < 0)
Either I am creating the new node incorrectly or accessing the elements inside in
the wrong way using pointers. Not sure how to correct this error
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来您正在尝试编写一个递归函数,因为它会调用自身。
由于函数具有带表达式的返回语句,因此其返回类型不应为
void
。此外,由于此 typedef,此参数声明
bintree *t
相当于struct bintreestruct **
但在函数中,您尝试将其用作具有类型
struct bintreestruct *
。在函数本身的这些调用中,
使用了不完整且排序不正确的参数列表。
考虑到所有这些,函数至少可以通过以下方式声明和定义。
请注意,对指针类型使用 typedef 声明是一个坏主意。它只会让代码的读者感到困惑。
It seems you are trying to write a recursive function because it calls itself.
As the function has return statements with expressions then its return type shall not be
void
.Also this parameter declaration
bintree *t
is equivalent tostruct bintreestruct **
due to this typedefBut within the function you are trying to use it as having the type
struct bintreestruct *
.And in these calls of the function itself
there are used incomplete and not correctly ordered lists of arguments.
Taking all this into account the function can be declared and defined at least the following way
Pay attention to that using the typedef declaration for the pointer type is a bad idea. It only confuses readers of the code.