【问题标题】:"Must use 'struct' tag to refer to type 'node'"“必须使用 'struct' 标签来引用类型 'node'”
【发布时间】:2015-05-26 01:59:15
【问题描述】:

这里发生了什么?我来了

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

在gx 和hx 行

typedef struct node {
    char * fx; // function
    node * gx; // left-hand side
    char * op; // operator
    node * hx; // right-hand side    
} node;

我也试过了

typedef struct node {
    char * fx; // function
    node * gx; // left-hand side
    char * op; // operator
    node * hx; // right-hand side
};

和

struct node {
    char * fx; // function
    node * gx; // left-hand side
    char * op; // operator
    node * hx; // right-hand side
};

和

typedef struct  {
    char * fx; // function
    node * gx; // left-hand side
    char * op; // operator
    node * hx; // right-hand side
} node;

和

typedef struct treeNode {
    char * fx; // function
    treeNode  * gx; // left-hand side
    char * op; // operator
    treeNode * hx; // right-hand side
} node;

我得到了所有这些错误。纯 C 中的正确语法又是什么?

【问题讨论】:

    标签: c data-structures


    【解决方案1】:

    我们看第一个sn-p:

    typedef struct node {
        char * fx; // function
        node * gx; // left-hand side
        char * op; // operator
        node * hx; // right-hand side    
    } node;
    

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

    typedef struct node {
               char * fx; // function
        struct node * gx; // left-hand side
               char * op; // operator
        struct node * hx; // right-hand side    
    } node;
    

    【讨论】:

      【解决方案2】:

      在c中,不能用结构名来引用struct,需要在名称前加struct,或者typedef就可以了,like

      typedef struct node node;
      
      struct node 
       {
          /* Whatever */
          node *link;
       };
      

      【讨论】:

        【解决方案3】:

        我是这样做的

        struct node {
            char        * fx;   // function
            struct node * gx;   // left-hand side
            char        * op;   // operator
            struct node * hx;   // right-hand side
        };
        typedef struct node node;
        

        【讨论】:

        • 是的,在看到 iharob 的回答之后。我可能会采用这种间距。混蛋。
        猜你喜欢
        • 2023-02-13
        • 1970-01-01
        • 1970-01-01
        • 2021-01-17
        • 1970-01-01
        • 2021-02-10
        • 2018-01-12
        • 1970-01-01
        • 2020-06-02
        相关资源
        最近更新 更多