简体   繁体   English

用于定义的typedef结构

[英]typedef struct used for definition

Why does using Node(..Node{..), as below, while defining a struct type gives a compile time error. 为什么在定义结构类型时使用Node(.. Node {..),如下所示,会产生编译时错误。

typedef struct node Node;

Node{
  int data;
  Node *next;
};

There is a very basic concept that is confusing me, please advise or refer me to a relevant link. 有一个非常基本的概念令我感到困惑,请建议或引用我的相关链接。

Typedef is used to provide an alias to some another type. Typedef用于为其他类型提供别名。 It is not a macro, it is not substituted for something at the place of use. 它不是一个宏,它不能替代使用地点的东西。

The correct definition might be: 正确的定义可能是:

typedef struct node {
  int data;
  struct node* next;
} Node;

At the very least, you need it to say 至少,你需要它说

typedef struct node Node;

struct Node{
  int data;
  Node *next;
};

However, you can do this: 但是,您可以这样做:

typedef struct {
  int data;
  struct Node *next;
}Node;  

Now you can easily create instances of the struct Node you have created by: 现在,您可以轻松地创建您创建的struct Node实例:

Node node, *pNode;  

Where node is the name of a new variable of type struct Node . 其中nodestruct Node类型的新变量的名称。
and *pNode is a pointer to the same, initialized by: *pNode是指向它的指针,初始化为:

  pNode = &node;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM