【问题标题】:C struct, pointers [duplicate]C结构,指针[重复]
【发布时间】:2011-12-21 20:12:00
【问题描述】:

可能重复:
C : pointer to struct in the struct definition

在我的初学者课程中,我们需要声明一个用于树搜索的结构。我需要在每个节点中引用相同类型的指针。这种减速似乎不正确,pnode 在这种情况下是未知的。我怎样才能实现我的目标?

typedef struct 
{ 
    int value;
    int right;
    int left;
    pnode nextleft;
    pnode nextright;
}node, *pnode;

【问题讨论】:

    标签: c pointers struct


    【解决方案1】:

    C Faq 是一个很好的参考。 http://c-faq.com/struct/selfref.html

    我倾向于在struct方法之前使用typedef

    http://c-faq.com/decl/selfrefstruct.html

       typedef struct a *APTR;
        typedef struct b *BPTR;
    
        struct a {
            int afield;
            BPTR bpointer;
        };
    
        struct b {
            int bfield;
            APTR apointer;
        };
    

    【讨论】:

      【解决方案2】:
      struct node
      {
          int value;
          int right;
          int left;
          struct node *nextleft;
          struct node *nextright;
      };
      

      这里,nodetag 命名空间内。

      【讨论】:

      • +1 如果你想避免使用struct nodestruct node * 来声明你的结构实例,你仍然可以在结构定义之后添加typedef struct node node; typedef struct node* pnode;。虽然为了代码清晰,我不希望这样做。
      猜你喜欢
      • 2018-11-03
      • 1970-01-01
      • 1970-01-01
      • 2013-03-29
      • 1970-01-01
      • 1970-01-01
      • 2021-05-03
      • 2021-04-11
      • 1970-01-01
      相关资源
      最近更新 更多