简体   繁体   English

使用本身就是结构的成员进行结构

[英]Struct with a member that is the struct itself

I have a struct with several members and one of them is the same struct itself. 我有一个具有多个成员的结构,其中一个成员本身是相同的结构。 What I want to do is have a pointer to a struct related to that same struct but is of the same type. 我想做的是有一个指向与该相同结构有关的结构的指针,但它的类型相同。 The compiler does not recognize the type when reading the struct members since it is still to be defined. 编译器在读取结构成员时无法识别类型,因为尚需定义。 Is there any alternative way to do what I want to happen? 有什么其他方式可以做我想做的事吗?

typedef struct _panels
{
    int id;
    // Dimensions
    double length;
    double height;

    // Global coordinates of origin of LCS
    double origX;
    double origY;
    double origZ;

    // Orientation of local x-axis wrt global X-axis
    double angle;

    // Panel reinforcement
    int nReinforcement;
    double *xReinf; // arbitrary spacing
    double sx;  // fixed spacing
    double xReinf0; // x-coordinate of first rebar

    // CHB unit
    CHBUnit *chb;

    // Openings
    int nOpenings;
    CHBOpening *openings;

    // Pointer to adjacent panels
    CHBPanel * left;    int leftPanelID;
    CHBPanel * right;   int rightPanelID;
}CHBPanel;

You should use the defined (incomplete in the definition of the structure) type struct _panels instead of CHBPanel , which is not defined yet, to declare pointers to the structure itself. 您应该使用已定义(结构定义中的不完整)类型的struct _panels而不是尚未定义的CHBPanel来声明指向结构本身的指针。

The last part 最后一部分

    CHBPanel * left;    int leftPanelID;
    CHBPanel * right;   int rightPanelID;

should be 应该

    struct _panels * left;    int leftPanelID;
    struct _panels * right;   int rightPanelID;

Alternative way: You can do the typedef before the declaration of the structure. 另一种方法:您可以在结构声明之前执行typedef

typedef struct _panels CHBPanel;
struct _panels
{
    int id;

    /* snipped */

    // Pointer to adjacent panels
    CHBPanel * left;    int leftPanelID;
    CHBPanel * right;   int rightPanelID;
};

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

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