结构中的"警告:空声明中无用的存储类说明符"

spa*_*ing 8 c error-handling struct compiler-errors linked-list

typedef struct item {
    char *text;
    int count;
    struct item *next;
};
Run Code Online (Sandbox Code Playgroud)

所以我有这个结构与上面定义的节点,但我得到下面的错误,我无法弄清楚什么是错的.

警告:空声明中无用的存储类说明符};

Car*_*ole 14

我不确定,但尝试这样:

typedef struct item {
  char *text;
  int count;
  struct item *next;
}item;
Run Code Online (Sandbox Code Playgroud)


fni*_*isi 8

typedef用于为 C 中的现有类型创建速记符号。它#define与它相似但又不同,typedef由编译器解释并提供比预处理器更高级的功能。

以其最简单的形式typedef给出

typedef existing_type new_type;
Run Code Online (Sandbox Code Playgroud)

例如,

typedef unsigned long UnsignedLong;
Run Code Online (Sandbox Code Playgroud)

例如,如果您将 的定义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;   
Run Code Online (Sandbox Code Playgroud)

进而

/* stddef.h */
typedef __size_t    size_t;
Run Code Online (Sandbox Code Playgroud)

这实际上意味着,size_t是 的别名unsigned long long,具体取决于您的机器拥有的 64 位模式(LP64、ILP64、LLP64)。

对于您的问题,您尝试定义新类型但未命名。不要让struct item {..}定义混淆你,它只是你声明的一种类型。如果你struct item {...}用一个基本类型替换整个,比如用 an int,然后重写你的typedef,你会得到这样的结果

typedef int; /* new type name is missing */
Run Code Online (Sandbox Code Playgroud)

正确的形式应该是

typedef struct item {...} Item;
Run Code Online (Sandbox Code Playgroud)

请参阅下面的示例了解不同的结构定义

#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;
}
Run Code Online (Sandbox Code Playgroud)


gna*_*729 5

typedef没用,因为你没有给它命名.您不能以任何方式使用typedef.这就是你得到警告的原因,因为typedef是没用的.