“warning: useless storage class specifier in empty declaration” in struct

前端 未结 4 668
悲&欢浪女
悲&欢浪女 2021-02-05 17:37
typedef struct item {
    char *text;
    int count;
    struct item *next;
};

So I have this struct with nodes defined as above, but Im getting the er

4条回答
  •  借酒劲吻你
    2021-02-05 18:05

    The struct is actually still usable without the warning if you remove the typedef keyword like this:

    struct item {
        char *text;
        int count;
        struct item *next;
    };
    

    You just need to include the 'struct' keyword in the variable declaration. i.e.

    struct item head;
    

    as other have pointed out if you include the name at the end of the struct definition then you can use it as the typedef and you get rid of the warning even without the struct keyword but this makes the first instance of 'item' superfluous i.e.

    typedef struct {
        char *text;
        int count;
        struct item *next;
    } item;
    
    item head;
    

    will also get rid of the warning.

提交回复
热议问题