[英]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;
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.