gpt4 book ai didi

c - 取消引用指向不完整类型结构的指针

转载 作者:太空宇宙 更新时间:2023-11-04 05:53:01 24 4
gpt4 key购买 nike

当我尝试编译时,我收到一条错误消息:“解引用指向不完整类型 struct Freunde 的指针”

那是我的结构:

typedef struct {
char *Name;
struct Freunde *next;
} Freunde;

错误发生在这里:

while (strcmp(Anfang->next->Name, Name) != 0)
Anfang = Anfang->next;

编辑///所以这里是我尝试运行的程序中的更多代码:

void add(Freunde* Anfang, char* Name) {
Freunde * naechster;

while (Anfang->next != NULL) {
Anfang = Anfang->next;
}
Anfang->next = (Freunde*) malloc(sizeof(Freunde));
naechster = Anfang->next;
naechster->Name = Name;
naechster->next = NULL;

}


int main() {
Freunde *liste;
liste = (Freunde*) malloc(sizeof(Freunde));

liste->Name = "Mert";
liste->next = NULL;

add(liste, "Thomas");
add(liste, "Markus");
add(liste, "Hanko");

Ausgabe(liste);

return 0;
}

最佳答案

主要问题是您将结构的 next 成员定义为 struct Freunde *next; 但没有 struct Freunde你的代码。

首先声明一个struct Freunde,像这样

struct Freunde
{
char *name;
struct Freunde *next;
};

然后您可以typedef,但您不必这样做

typedef struct Freunde Freunde;

还有:

  1. 不要为these reasons 转换malloc() 的返回值
  2. 始终检查 malloc() 是否返回 NULL

关于c - 取消引用指向不完整类型结构的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34686253/

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