简体   繁体   中英

dereferencing Pointer to incomplete type struct

When I try to compile, i get an Error saying :" dereferencing Pointer to incomplete type struct 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; but there is no struct Freunde in your code.

First declare a struct Freunde , like this

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

and then you could typedef , but you don't have to

typedef struct Freunde Freunde;

Also:

  1. Do not cast the return value of malloc() for these reasons
  2. Always check that malloc() did not return 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 struct {
    char *Name;
    struct Freunde *next;
} Freunde;

As explained, when you declare the member pointer struct Freunde *next; , the compiler has no idea what Freunde is yet. 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; it is fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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