Dereference pointer to incomplete type
在处理如下代码时,编译器报错Dereference pointer to incomplete type。
a.c:
1 struct point {
2 double x;
3 double y;
4 };
5
6 ... other code ...
a.h:
1 typedef struct point *Point;
main.c:
1 #include "a.h"
2
3 int main(void)
4 {
5 Point p;
6 p->x = 2.3; /* Dereference pointer to incomplete type */
7 return 0;
8 }
原因是Point声明在a.h中,main.c包含了a.h,所以在main.c中,Point的scope
为第一行到文件末尾。struct point的scope为a.c的第4行到该文件末尾。
因为main.c并不在struct point的scope内,所以当你在main.c中试图
对(struct point *) p进行dereference的时候,编译器并不知道此时struct point
在main.c的意思,所以会报错。
解决方法也简单,在main.c中dereference (struct point *) p之前,声明struct point
即可。修改后的main.c如下:
1 #include "a.h"
2
3 struct point {
4 double x;
5 double y;
6 };
7
8 int main(void)
9 {
10 Point p;
11 p->x = 2.3; /* problem gone! */
12 return 0;
13 }
以上。