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 };
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 asfor instance,
For example, if you trace the definition of
size_t
back to its root, you will see thatand then
which actually means,
size_t
is an alias forunsigned 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 wholestruct item {...}
with a basic type, say with anint
, and rewrite yourtypedef
, you would end up something like thisthe correct form should be
See the examples below for different structure definitions
The struct is actually still usable without the warning if you remove the typedef keyword like this:
You just need to include the 'struct' keyword in the variable declaration. i.e.
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.
will also get rid of the warning.
The typedef is useless because you didn't give it a name. You cannot use the typedef in any way. That's why you get a warning, because the typedef is useless.
I'm not sure, but try like that :