结构中的“警告:空声明中无用的存储类说明符”
typedef struct item { char *text; int count; struct item *next; };
所以我有这个结构与上面定义的节点,但我得到下面的错误,我无法弄清楚什么是错的。
警告:空声明中无用的存储类说明符};
我不确定,但尝试这样:
typedef struct item { char *text; int count; struct item *next; }item;
typedef没用,因为你没有给它命名。 您不能以任何方式使用typedef。 这就是你得到警告的原因,因为typedef没用。
如果你删除typedef关键字,结构实际上仍然可用而没有警告:
struct item { char *text; int count; struct item *next; };
您只需要在变量声明中包含’struct’关键字。 即
struct item head;
正如其他人已经指出的那样,如果在结构定义的末尾包含名称,则可以将其用作typedef,即使没有struct关键字,也可以消除警告,但这会使“item”的第一个实例变得多余,即
typedef struct { char *text; int count; struct item *next; } item; item head;
也将摆脱警告。
typedef
用于为C中的现有类型创建简写表示法。它与#define
类似,但与之不同, typedef
由编译器解释,并提供比预处理器更高级的function。
最简单的forms是typedef
typedef existing_type new_type;
例如,
typedef unsigned long UnsignedLong;
例如,如果您将size_t
的定义追溯到其根目录,您将看到它
/* 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;
然后
/* stddef.h */ typedef __size_t size_t;
实际上, size_t
是unsigned long long
的别名,具体取决于您的机器具有的64位模式(LP64,ILP64,LLP64)。
对于您的问题,您尝试定义新类型但不命名。 不要让struct item {..}
定义让您感到困惑,它只是您声明的类型。 如果用基本类型替换整个struct item {...}
,比如使用int
,并重写你的typedef
,你最终会得到像这样的东西
typedef int; /* new type name is missing */
应该是正确的forms
typedef struct item {...} Item;
有关不同的结构定义,请参阅以下示例
#include /* 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; }