"必须使用'struct'标签来引用'node'类型"

Mic*_*dge 3 c data-structures

这里发生了什么?我越来越

必须使用'struct'标签来引用'node'类型

gxhx线

typedef struct node {
    char * fx; // function
    node * gx; // left-hand side
    char * op; // operator
    node * hx; // right-hand side    
} node;
Run Code Online (Sandbox Code Playgroud)

我也试过了

typedef struct node {
    char * fx; // function
    node * gx; // left-hand side
    char * op; // operator
    node * hx; // right-hand side
};
Run Code Online (Sandbox Code Playgroud)

struct node {
    char * fx; // function
    node * gx; // left-hand side
    char * op; // operator
    node * hx; // right-hand side
};
Run Code Online (Sandbox Code Playgroud)

typedef struct  {
    char * fx; // function
    node * gx; // left-hand side
    char * op; // operator
    node * hx; // right-hand side
} node;
Run Code Online (Sandbox Code Playgroud)

typedef struct treeNode {
    char * fx; // function
    treeNode  * gx; // left-hand side
    char * op; // operator
    treeNode * hx; // right-hand side
} node;
Run Code Online (Sandbox Code Playgroud)

我得到了所有这些错误.普通C中的正确语法是什么?

Iha*_*imi 10

在c中,您不能使用结构名称来引用struct,您需要struct在名称之前添加,或者您可以使用typedef

typedef struct node node;

struct node 
 {
    /* Whatever */
    node *link;
 };
Run Code Online (Sandbox Code Playgroud)


jwo*_*der 5

让我们看一下第一个片段:

typedef struct node {
    char * fx; // function
    node * gx; // left-hand side
    char * op; // operator
    node * hx; // right-hand side    
} node;
Run Code Online (Sandbox Code Playgroud)

gxhx在语句完成之前出现在struct node/ node类型定义的中间typedef。在程序中,node这不是有效的类型名称,因为typedef还没有结束(并且与C ++不同,编写struct node { ... };不会自动node生成类型名称)。然而,struct node 就是在这一点上有效的类型名称(只要你只使用它的指针类型),所以为了申报gxhx正确,你需要写:

typedef struct node {
           char * fx; // function
    struct node * gx; // left-hand side
           char * op; // operator
    struct node * hx; // right-hand side    
} node;
Run Code Online (Sandbox Code Playgroud)