typedef struct A A;
typedef struct A* AList;
struct A{
char* name;
AList *next;
};
AList create(char *name)
{
AList new = (AList)malloc(sizeof(AList));
new->name = name;
new->next = NULL;
return new;
}
void add(char* name, AList *aList)
{
AList list = *aList;
if (list == NULL)
{
list = create(name);
*aList = list;
}
else
{
while (list->next != NULL)
list = list->next;
list->next = create(name);
}
}
你好!这是我遇到问题的代码。一切正常,我只在代码的最后两行中得到“来自不兼容指针类型的赋值”,我不知道为什么。如果我更改指针,使它们具有兼容的类型(在我看来,即 list = *list->next;),我会遇到段错误。我怎样才能解决这个问题? 谢谢您的帮助, 穆科
最佳答案
改变这个
struct A{
char* name;
AList *next;
};
到此
struct A{
char* name;
AList next;
};
函数create
的返回类型是AList
,定义为A*
。但是 A::next
的类型是 AList*
,即 A**
,因此类型不兼容。
关于c - C 中的链表/指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36114120/