C语言 “警告:空声明中无用的存储类说明符”在结构中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37229811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"warning: useless storage class specifier in empty declaration" in struct
提问by spacing
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 };
警告:空声明中无用的存储类说明符 };
回答by Cariamole
I'm not sure, but try like that :
我不确定,但试试这样:
typedef struct item {
char *text;
int count;
struct item *next;
}item;
回答by gnasher729
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.
typedef 没有用,因为你没有给它一个名字。您不能以任何方式使用 typedef。这就是您收到警告的原因,因为 typedef 没有用。
回答by codeasaurus
The struct is actually still usable without the warning if you remove the typedef keyword like this:
如果您像这样删除 typedef 关键字,则该结构实际上仍然可用而不会发出警告:
struct item {
char *text;
int count;
struct item *next;
};
You just need to include the 'struct' keyword in the variable declaration. i.e.
您只需要在变量声明中包含“struct”关键字。IE
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 关键字,您也可以消除警告,但这使得“项目”的第一个实例变得多余,即
typedef struct {
char *text;
int count;
struct item *next;
} item;
item head;
will also get rid of the warning.
也将摆脱警告。
回答by fnisi
typedefis used to create a shorthand notation for an existing type in C. It is similar to #definebut unlike it, typedefis interpreted by the compiler and offers more advanced capabilities than the preprocessor.
typedef用于为 C 中的现有类型创建速记符号。它#define与它相似但又不同,typedef由编译器解释并提供比预处理器更高级的功能。
With its simplest form, typedefis given as
以其最简单的形式typedef给出
typedef existing_type new_type;
for instance,
例如,
typedef unsigned long UnsignedLong;
For example, if you trace the definition of size_tback to its root, you will see that
例如,如果您将 的定义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;
and then
进而
/* stddef.h */
typedef __size_t size_t;
which actually means, size_tis an alias for unsigned long long,depending on the 64-bit modal (LP64, ILP64, LLP64) your machines has.
这实际上意味着,size_t是 的别名unsigned long long,具体取决于您的机器拥有的 64 位模式(LP64、ILP64、LLP64)。
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
对于您的问题,您尝试定义新类型但未命名。不要让struct item {..}定义混淆你,它只是你声明的一种类型。如果你struct item {...}用一个基本类型替换整个,比如用 an int,然后重写你的typedef,你会得到这样的结果
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;
}

