我正在尝试用 C 语言创建一个链表结构。但我不太确定出了什么问题。我的错误是:
linked.c:6:2: error: unknown type name ‘linkedList’
linked.c: In function ‘makeList’:
linked.c:30:2: error: ‘first’ undeclared (first use in this function)
linked.c:30:2: note: each undeclared identifier is reported only once for each function it appears in
linked.c: In function ‘addToList’:
linked.c:36:9: error: used struct type value where scalar is required
linked.c:43:13: error: incompatible types when assigning to type ‘int *’ from type ‘linkedList’
如果有人能看到问题所在并向我解释,我将不胜感激。我的代码如下。
#include <stdio.h>
typedef struct linkedList
{
int first;
linkedList* rest;
} linkedList;
linkedList makeList(int a, int b, int c);
void addToList(linkedList* ll, int a);
int main()
{
linkedList ll = makeList(1,3,5);
addToList(&ll, 7);
addToList(&ll, 9);
return 0;
}
linkedList makeList(int a, int b, int c)
{
linkedList ll;
ll.first = a;
linkedList second;
second.first = b;
linkedList third;
third.first = c;
third.rest = NULL;
second.rest = &c;
first.rest = &b;
return first;
}
void addToList(linkedList* ll, int a)
{
while (*ll)
{
if (ll->rest == NULL)
{
linkedList newL;
newL.first = a;
newL.rest = NULL;
ll->rest = newL;
break;
} else
{
continue;
}
}
}
C编译器没有完整的typedef
of linkedList
在您尝试在您的应用程序中使用它之前struct
。您有几个选择:
typedef struct linkedList
{
int first;
struct linkedList* rest;
} linkedList;
Or:
typedef struct linkedList linkedList; // C allows this forward declaration
struct linkedList
{
int first;
linkedList* rest;
};
这是你的起点。
其他问题包括但不限于:
- Your
makeList
函数引用变量first
但它似乎没有在任何地方定义。
-
ll->rest = newL;
分配一个类型linkedList
to a pointer to linkedList
(linkedList *
)您不能将值分配给指向值的指针。编译器错误信息linked.c:43:13:...
指出这一点。它需要是ll->rest = &newL;
... 然而...
-
newL
对于函数来说是本地的addToList
,因此您不能将其地址分配给持久列表项,因为当代码离开该块时它将超出范围。
- In
addToList
您正在将指向整数的指针分配给一个保存指向的变量linkedList
, e.g., second.rest = &c;
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)