C &引用;警告:空声明中的存储类说明符无效“;结构中
所以我有这个结构,上面定义了节点,但是我得到了下面的错误,我无法找出是什么错误 警告:空声明中的存储类说明符无效 };C &引用;警告:空声明中的存储类说明符无效“;结构中,c,struct,error-handling,compiler-errors,linked-list,C,Struct,Error Handling,Compiler Errors,Linked List,所以我有这个结构,上面定义了节点,但是我得到了下面的错误,我无法找出是什么错误 警告:空声明中的存储类说明符无效 }; 我不确定,但试着这样做: typedef struct item { char *text; int count; struct item *next; }; typedef是无用的,因为您没有给它命名。您不能以任何方式使用typedef。这就是为什么会收到警告,因为typedef是无用的 如果删除typedef关键字,该结构实际上仍然可用,没有警告:
我不确定,但试着这样做:
typedef struct item {
char *text;
int count;
struct item *next;
};
typedef是无用的,因为您没有给它命名。您不能以任何方式使用typedef。这就是为什么会收到警告,因为typedef是无用的 如果删除typedef关键字,该结构实际上仍然可用,没有警告:
typedef struct item {
char *text;
int count;
struct item *next;
}item;
您只需要在变量声明中包含'struct'关键字。i、 e
struct item {
char *text;
int count;
struct item *next;
};
正如其他人所指出的,如果在结构定义的末尾包含名称,则可以将其用作typedef,即使没有struct关键字,也可以消除警告,但这使得“item”的第一个实例多余,即
struct item head;
也将消除警告。
typedef
用于为C中的现有类型创建简写符号。它类似于#define
,但与之不同,typedef
由编译器解释,并提供比预处理器更高级的功能
对于其最简单的形式,typedef
如下所示
typedef struct {
char *text;
int count;
struct item *next;
} item;
item head;
比如说,
typedef existing_type new_type;
例如,如果将size\t
的定义追溯到其根,您将看到
typedef unsigned long UnsignedLong;
然后
/* 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;
这实际上意味着,size\u t
是unsigned long-long
的别名,具体取决于机器的64位模式(LP64、ILP64、LLP64)
对于您的问题,您尝试定义一个新类型,但不命名它。不要让struct item{..}
定义迷惑您,它只是您正在声明的一种类型。如果用基本类型替换整个结构项{…}
,比如用int
,然后重写typedef
,您将得到如下结果
/* stddef.h */
typedef __size_t size_t;
正确的形式应该是
typedef int; /* new type name is missing */
有关不同的结构定义,请参见下面的示例
typedef struct item {...} Item;
#包括
/*这里定义了一个新类型,即Item*/
类型定义结构项{
字符*文本;
整数计数;
struct item\u t*next;/*您不能在此处使用item*/
}项目;
/*结构定义如下*/
结构项{
字符*文本;
整数计数;
结构项*下一步;
};
/*匿名结构
*然而,你不能在这里自我参照
*/
结构{
int i;
字符c;
}安农;
内部主(空){
/*指向结构项实例的指针*/
结构项*pi;
/*结构项目_t*iI的简写*/
项目*二;
/*匿名结构*/
anon.i=9;
anon.c='x';
返回0;
}
您忘记给它命名:typedef结构项{char*text;int count;结构项*next;}tralalala代码>我认为在结构中使用typedef处理指针是不可取的?即使我不需要typedef,我也应该使用它吗?一般来说,你不需要typedef。在typedef后面隐藏指针会让人更加困惑代码>与从结构大括号中声明指针相同?
#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;
}