incomplete type 'struct' error in C

12,186

Solution 1

You have forgotten to #include "client.h", so the definition of struct client is not known in client.c, hence struct client denotes an incomplete type there.

Solution 2

Sorry, but you need to include client.h, the compiler only compiles what he is told to...

Share:
12,186
user1667175
Author by

user1667175

Updated on June 05, 2022

Comments

  • user1667175
    user1667175 almost 2 years

    I have this issue and can't see where the error is so I'm hoping someone can help address it. The error I get from compiling the source is:

    client.c:15:54: error: invalid application of ‘sizeof’ to incomplete type ‘struct client’
    

    I have the struct definition inside a header file - client.h:

    #ifndef PW_CLIENT
    #define PW_CLIENT
    
    #include <event2/listener.h>
    #include <event2/bufferevent.h>
    #include <event2/buffer.h>
    
    #include <arpa/inet.h>
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <errno.h>
    
    struct client {
        int id;
        struct bufferevent *bev;
    
        struct client *prev;
        struct client *next;
    };
    
    struct client *client_head = NULL;
    
    struct client* client_connect(struct bufferevent *bev);
    #endif
    

    And here is the source of client.c:

    #include <event2/listener.h>
    #include <event2/bufferevent.h>
    #include <event2/buffer.h>
    
    #include <arpa/inet.h>
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <errno.h>
    
    struct client* client_connect(struct bufferevent *bev) {
        // Client info
        struct client *c = (struct client*)malloc(sizeof(struct client));
        if (c == NULL) {
            // error allocating memory
        } else {
        if (client_head == NULL) {
            // initialize list addresses
            c->prev = c->next = NULL;
    
            // set connection id
            c->id = 0;
        } else {
            // set list addresses
            client_head->next = c;
            c->prev = client_head;
            c->next = NULL;
            client_head = c;
    
            // set connection id
            c->id = (c->prev->id + 1);
        }
    
            // initialize user vars
            c->bev = bev;
        }
    
        return c;
    }
    

    Thanks!

  • user1667175
    user1667175 over 11 years
    I have it in another .c file, didn't know that I have to include it in client.c too. Have tried including it before and the compiler still gave me an error, but after putting it in client.c again it compiles well o_O Thanks!