gpt4 book ai didi

c - C 中的链表/指针

转载 作者:行者123 更新时间:2023-11-30 17:03:34 25 4
gpt4 key购买 nike

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/

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