解除指向不完整类型struct

时间:2016-01-08 21:19:59

标签: c pointers struct

当我尝试编译时,我得到一个错误说:"解引用指向不完整类型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;
}

2 个答案:

答案 0 :(得分:5)

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

首先声明struct Freunde,就像这样

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

然后你可以typedef,但你不必

typedef struct Freunde Freunde;

此外:

  1. 请勿为these reasons
  2. 转换malloc()的返回值
  3. 始终检查malloc()是否未返回NULL

答案 1 :(得分:0)

问题的另一个方面,或者另一种思考方式是,您正在从结构创建.aad button,.aad input,.aad textarea { width: 50%; } 并尝试将指向该结构类型的指针作为成员包含在内。

typedef

如上所述,当您声明成员指针typedef struct { char *Name; struct Freunde *next; } Freunde; 时,编译器不知道struct Freunde *next;是什么。因此错误。

要解决此问题,您可以按照其他答案中的说明进行操作,也可以在声明中包含结构名称标记。

Freunde

在这种情况下,typedef struct Freunde { char *Name; struct Freunde *next; } Freunde; 告诉编译器有一个名为struct Freunde {...的结构,所以当它到达你的成员Freunde时就可以了。

相关问题