0

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;
}
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
Scarh
  • 29
  • 1
  • 8

2 Answers2

5

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.
Community
  • 1
  • 1
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • Thank you man, it worked out. But why do I write "Freunde" twice and not typedef struct Freunde; – Scarh Jan 08 '16 at 21:30
  • Thanks alot ! (just fillin up to 15 characters) – Scarh Jan 08 '16 at 21:36
  • @Scarh `typedef` introduces an alias for an existing type. You are adding the alias `Fruende` for the type `struct Fruende`. Your suggestion would have no effect (it's syntactically valid, by accident, but it doesn't introduce any new aliases). – M.M Jan 08 '16 at 22:28
0

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.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • `Freunde` is different from `struct Freunde`. Also, `struct {...} Freunde;` without the `typedef` declares a **variable**. – user253751 Jan 08 '16 at 22:37
  • Thank you, I liked that method more than just typing typedef struct Freunde Freunde; after my struct. – Scarh Jan 09 '16 at 03:57