结构的成员是结构本身

Struct with a member that is the struct itself

我有一个包含多个成员的结构,其中一个是同一个结构本身。我想要做的是有一个指向与同一结构相关但类型相同的结构的指针。编译器在读取结构成员时无法识别类型,因为它仍有待定义。有没有其他方法可以做我想发生的事情?

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;

您应该使用已定义(在结构定义中不完整)类型 struct _panels 而不是尚未定义的 CHBPanel 来声明指向结构本身的指针。

最后一部分

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

应该是

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

替代方法:您可以在声明结构之前执行 typedef

typedef struct _panels CHBPanel;
struct _panels
{
    int id;

    /* snipped */

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