简体   繁体   English

将指针解引用到不完整的类型结构

[英]dereferencing Pointer to incomplete type struct

When I try to compile, i get an Error saying :" dereferencing Pointer to incomplete type struct Freunde" 当我尝试编译时,出现错误消息:“将指针解引用为不完整的类型结构Freunde”

Thats my struct: 那就是我的结构:

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

The Error happens here: 错误发生在这里:

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

Edit/// So here is some more Code from the Programm I do try to run: 编辑///因此,这是我尝试运行的程序中的更多代码:

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;
}

The main problem is that you defined the next member of your structure as struct Freunde *next; 主要问题是您将结构的next成员定义为struct Freunde *next; but there is no struct Freunde in your code. 但您的代码中没有struct Freunde

First declare a struct Freunde , like this 首先声明一个struct Freunde ,像这样

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

and then you could typedef , but you don't have to 然后您可以输入typedef ,但不必

typedef struct Freunde Freunde;

Also: 也:

  1. Do not cast the return value of malloc() for these reasons 由于这些原因,请勿转换malloc()的返回值
  2. Always check that malloc() did not return NULL . 始终检查malloc()没有返回NULL

Another aspect of the problem, or another way to think about it, is you are creating a typedef from a struct and attempting to include a pointer to that struct type as a member. 问题的另一方面,或另一种思考方式,是您从一个结构创建一个typedef ,并尝试将指向该结构类型的指针作为成员。

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

As explained, when you declare the member pointer struct Freunde *next; 如前所述,当声明成员指针struct Freunde *next; , the compiler has no idea what Freunde is yet. ,编译器还不知道什么是Freunde Thus the error. 因此错误。

To remedy this, you can either do as described in the other answer, or include a struct name tag in your declaration. 为了解决这个问题,您可以按照其他答案中的描述进行操作,也可以在声明中包含结构名称标签。

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

In this case struct Freunde {... tells the compiler that there is a struct named Freunde , so when it reaches your member struct Freunde *next; 在这种情况下, struct Freunde {...告诉编译器有一个名为Freunde的结构,因此当到达您的成员时, struct Freunde *next; it is fine. 没事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM