您正在调用您的结构Node 并定义node_t 类型。然后您使用node_t,就好像它是结构的名称而不是类型。
试试这个
typedef struct node {
int value;
struct node *next;
} Node;
或者
typedef struct node Node;
struct node {
int value;
Node *node;
};
如果你叫它struct Node,那么
struct Node {
int value;
/* The compiler doesn't know what `struct Node' is yet */
struct Node *next;
/* But you can always declare pointers, even of types the compiler
* doesn't know everything about. Because the size of a pointer
* does not depend on the type of the pointee.
*/
};
在您的示例中,情况更糟。你typedefed 了一些编译器理解的新类型,要使用它你不能使用struct。 typedefing 背后的整个想法是你定义了一个新类型,所以假设如下
typedef struct Node node;
然后声明node 类型的指针(注意,再次node IS A TYPE),
node *anode;
但你尝试过类似
struct node *anode;
这是错误的,因为上面的代码中没有struct node,而是struct Node。
您的代码中的另一个错误是,当编译器找到 node_t 类型时,
struct node_t *next;
这已经是错误的,因为如果类型是在结构之前定义的,那么可能像这样
typedef struct Node node_t
在node_t 类型上使用struct 仍然是错误的,因为对于编译器而言,node_t 不是struct,它是一个新类型,而它又只是struct Node 的别名.
根据我的经验,类型定义结构是麻烦多于好处。输入struct Something 而不仅仅是Something 并不难。它还具有更明确的好处,因此如果其他程序员阅读您的代码,他们将立即知道Something 是struct。
注意:我故意将名称更改为 node,因为在您自己定义的类型后面加上 _t 被认为是不好的做法。这不一定是一件坏事,但多年来我一直在使用它,我养成了一些习惯,其中一个习惯是不要使用_t 作为我自己定义的类型的后缀。顺便说一句,如果它们会大大提高可读性,那么它们仅存在于我的代码中。否则,我只需使用带有 struct 关键字的结构名称。