C requires that a struct or union has at least one member

10,593

Solution 1

The error message is strange, but

typedef struct LinkedList
{
    LinkedList *next;
    Data *data;
} LinkedList;

LinkedList is not a known type in the definition, it is only known after the definition is complete, it must be

typedef struct LinkedList
{
    struct LinkedList *next;
    Data *data;
} LinkedList;

Solution 2

Header files aren't compiled on their own. Their only use is being #included elsewhere.

The error in your header is this part:

typedef struct LinkedList  // <- defines the type 'struct LinkedList'
{
    LinkedList *next;      // <- attempts to use the type 'LinkedList', which isn't defined at this point
} LinkedList;              // <- defines the type 'LinkedList', or it would if the previous code were correct

There are a few possible ways to fix this:

// forget about 'LinkedList'; just use 'struct LinkedList' everywhere
struct LinkedList {
    struct LinkedList *next;
};

// use 'struct LinkedList' in its own definition but provide typedef to users
struct LinkedList {
    struct LinkedList *next;
};
typedef struct LinkedList LinkedList;

// like the previous version, but combine struct + typedef
typedef struct LinkedList {
    struct LinkedList *next;
} LinkedList;

And my favorite:

// establish alias first, then use it everywhere
typedef struct LinkedList LinkedList;
struct LinkedList {
    LinkedList *next;
};
Share:
10,593
Kevin
Author by

Kevin

Updated on November 23, 2022

Comments

  • Kevin
    Kevin over 1 year

    I'm getting a bunch of weird errors from the compiler when I try to compile this code.

    I'm just trying to write a basic linked list, and the header file compiles fine when run on its own but when I compile the c file everything falls apart

    linkedlist.h

    typedef struct Data
    {
        char *title;
    } Data;
    
    typedef struct LinkedList
    {
        LinkedList *next;
        Data *data;
    } LinkedList;
    
    LinkedList *createnew(void);
    void destroylist(LinkedList *old);
    Data *adddata(void);
    void destroydata(Data *old);
    

    LinkedList.c

    #include <stdlib.h>
    #include "linkedlist.h"
    
    LinkedList *createnew(void) {
        LinkedList *newnode = (LinkedList *) malloc(sizeof(LinkedList));
        newnode->data = adddata();
        newnode->next = NULL;
        return newnode;
    }
    
    void destroylist(LinkedList *old) {
        destroydata(old->data);
        free(old);
        return;
    }
    
    Data *adddata(void) {
        Data *newdata = (Data *) malloc(sizeof(Data));
        newdata->title = NULL;
        return newdata;
    }
    
    void destroydata(Data *old) {
        free(old->title);
        free(old);
        return;
    }
    

    and finally what the compiler spits out

    linkedlist.h(8): error C2016: C requires that a struct or union has at least one member
    linkedlist.h(8): error C2061: syntax error : identifier 'LinkedList'
    linkedlist.h(10): error C2059: syntax error : '}'
    linkedlist.h(12): error C2143: syntax error : missing '{' before '*'
    linkedlist.h(13): error C2143: syntax error : missing ')' before '*'
    linkedlist.h(13): error C2143: syntax error : missing '{' before '*'
    linkedlist.h(13): error C2059: syntax error : ')'
    linkedlist.c(4): error C2143: syntax error : missing '{' before '*'
    linkedlist.c(5): error C2065: 'LinkedList' : undeclared identifier
    linkedlist.c(5): error C2065: 'newnode' : undeclared identifier
    linkedlist.c(5): error C2059: syntax error : ')'
    linkedlist.c(6): error C2065: 'newnode' : undeclared identifier
    linkedlist.c(6): error C2223: left of '->data' must point to struct/union
    linkedlist.c(7): error C2065: 'newnode' : undeclared identifier
    linkedlist.c(7): error C2223: left of '->next' must point to struct/union
    linkedlist.c(8): error C2065: 'newnode' : undeclared identifier
    linkedlist.c(8): warning C4047: 'return' : 'int *' differs in levels of indirection from 'int'
    linkedlist.c(11): error C2143: syntax error : missing ')' before '*'
    linkedlist.c(11): error C2143: syntax error : missing '{' before '*'
    linkedlist.c(11): error C2059: syntax error : ')'
    linkedlist.c(11): error C2054: expected '(' to follow 'old'
    

    I'm confused because it looks like it finds both files but then it has trouble with the header even though it works fine alone. Any help would be great. Thanks