大家好!
我在尝试测试 Clever Frog 游戏的代码时遇到以下错误: 错误:取消引用指向不完整类型的指针
“完整代码”位于 pastebin.com - here (不会过期)。但我认为通过下面的解释,任何人都可以理解。注意:我还没有实现删除分配的内存和其他东西的功能。
我在 1.c 文件中定义了一个结构:
#include "1.h"
...
struct test {
int a;
};
...
我有一个 1.h,其中有 typedef 使用它:
...
typedef struct test testT;
...
然后我有一个函数,其中有一个参数取决于 testT,它在 2.c 中:
...
void funcTest(testT **t, int *size, ..){
/* another function that creates mem.space/alocate memory based enter code here`on the need of size above */
createMem(t,*size); /* void createMem(testT **t, int size); */
t[0]->a = 0; /*ERROR HERE*/
/* ... more code ... */
}
...
2.h文件是这样的:
...
void funcTest(testT **t, int *size, ..);
...
我将按以下方式在主程序中传递一个testT *var:
...
testT *varTest; int size;
funcTest(&varTest, &size);
...
奇怪的是,当我在 1.h 文件中使用 struct test 时,代码会编译(从 1.c 中删除 struct test - 这是错误的)。但是,在运行编译后的程序时,错误发生的地方正是t[0]->a的地方。
我已经尝试了“一切”,但没有任何效果 :( 我相信这是非常愚蠢的事情,所以如果有人知道什么,请告诉我 :D 谢谢!
最佳答案
当您尝试访问 t[0]
结构的 a
成员时,编译器需要知道该结构的外观(例如,查看是否甚至是其中的任何 a
成员)。由于在编译 2.c
时您没有将 struct test
类型定义放在编译器可以看到它的任何位置,因此您会收到一个错误。编译器不知道 struct test
包含什么。
如果将 struct test
的定义放在 1.h
中,编译器会看到该类型的外观并可以使用结构成员。
只需将完整的类型定义放在 1.h
中,这是它应该在的地方。
关于c - 错误 : In C, 在结构指针中得到错误 "dereferencing pointer to incomplete type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5275501/