Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"warning: useless storage class specifier in empty declaration" in struct

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

So I have this struct with nodes defined as above, but Im getting the error below and Im not able to figure out whats wrong.

warning: useless storage class specifier in empty declaration };

like image 248
spacing Avatar asked May 14 '16 17:05

spacing


2 Answers

I'm not sure, but try like that :

typedef struct item {
  char *text;
  int count;
  struct item *next;
}item;
like image 149
Cariamole Avatar answered Sep 18 '22 03:09

Cariamole


typedef is used to create a shorthand notation for an existing type in C. It is similar to #define but unlike it, typedef is interpreted by the compiler and offers more advanced capabilities than the preprocessor.

With its simplest form, typedef is given as

typedef existing_type new_type;

for instance,

typedef unsigned long UnsignedLong;

For example, if you trace the definition of size_t back to its root, you will see that

/* sys/x86/include/_types.h in FreeBSD */
/* this is machine dependent */
#ifdef  __LP64__
typedef unsigned long       __uint64_t;
#else
__extension__
typedef unsigned long long  __uint64_t;
#endif
...
...
typedef __uint64_t  __size_t;   

and then

/* stddef.h */
typedef __size_t    size_t;

which actually means, size_t is an alias for unsigned long long,depending on the 64-bit modal (LP64, ILP64, LLP64) your machines has.

For your question, you attempt to define a new type but do not name it. Don't let the struct item {..} definition confuse you, it is just a type you are declaring. If you replace the whole struct item {...} with a basic type, say with an int, and rewrite your typedef, you would end up something like this

typedef int; /* new type name is missing */

the correct form should be

typedef struct item {...} Item;

See the examples below for different structure definitions

#include <stdio.h>

/* a new type, namely Item, is defined here */
typedef struct item_t {
  char *text;
  int count;
  struct item_t *next; /* you canot use Item here! */
} Item;

/* a structure definition below */
struct item {
  char *text;
  int count;
  struct item *next;
};

/* an anonymous struct
* However, you cannot self-refence here 
*/
struct {
  int i;
  char c;
} anon;

int main(void) {
  /* a pointer to an instance of struct item */
  struct item *pi;

  /* Shorthand for struct item_t *iI */
  Item *iI;

  /* anonymoous structure */
  anon.i = 9;
  anon.c = 'x';
  return 0;
}
like image 33
fnisi Avatar answered Sep 17 '22 03:09

fnisi