Question

typedef struct{
    char name[25];
    int yearOfBirth;
    int district;
    char gender;
    int age;

    CitizenType *next;

}CitizenType;

When I try to make a Linked List in this format in Visual Studio, I get all sorts of errors relating to syntax. (102 in total) but when I comment out the CitizenType *next; I get no errors. I realize it has something to do with referencing the structure before it has been completely declared, but I have no idea how to fix this.

Was it helpful?

Solution

Try this:

typedef struct node{
    char name[25];
    int yearOfBirth;
    int district;
    char gender;
    int age;

    struct node *next;

}CitizenType;

Check this stack overflow answer for more information about self referenced structs in C. From the answer:

A CitizenType cannot contain another CitizenType as it becomes a never-ending recursion.

Hope it helps!

OTHER TIPS

Declare the typedef before (and separately from) the structure.

typedef struct citizen_type CitizenType;

struct citizen_type {
    ...
    CitizenType *next;
};

The problem is that

CitizenType

enters into the namespace of types only after the structure ends.

So you can use the new type only after its declaration.

You can use instead of it a struct name (giving a name to the struct), or declaring the type before to declare the structure with a name, as in previous post.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top