简体   繁体   中英

typedef struct used for definition

Why does using Node(..Node{..), as below, while defining a struct type gives a compile time error.

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. 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:

Node node, *pNode;  

Where node is the name of a new variable of type struct Node .
and *pNode is a pointer to the same, initialized by:

  pNode = &node;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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