我正在阅读《现代C》这本书,很惊讶地发现以下代码有效
typedef struct point point;
struct point {
int x;
int y;
};
int main() {
point p = { .x = 1, .y = 2 };
}
Run Code Online (Sandbox Code Playgroud)
但是,这本书没有详细介绍。这是如何运作的?为什么point在main()指typedef自struct point定义之后呢?
该行typedef struct point point;有两件事:
struct pointstruct point叫做point。如果您需要在结构完全定义之前就知道其退出,则前向声明很有用,例如:
typedef struct x X;
typedef struct y {
int a;
X *x;
} Y;
struct x {
int b;
Y *y;
};
Run Code Online (Sandbox Code Playgroud)