C语言 错误:在 C 中,在结构指针中出现错误“取消对不完整类型的指针的引用”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5275501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: In C, got the error "dereferencing pointer to incomplete type" in a struct pointer
提问by Shuryon
Hello Everybody!
大家好!
I got the following error, while trying to test a code for the game Clever Frog: error: dereferencing pointer to incomplete type
我在尝试测试游戏 Clever Frog 的代码时遇到以下错误: 错误:取消引用指向不完整类型的指针
The 'full code' is at pastebin.com - here(won't expire). But I think that with the explanation below, anybody can understands. Note: I haven't implemented yet the function that will erase the allocated memory and other things.
“完整代码”位于 pastebin.com -这里(不会过期)。但我想通过下面的解释,任何人都可以理解。注意:我还没有实现擦除分配的内存和其他东西的功能。
I have a struct defined in a 1.c file:
我在 1.c 文件中定义了一个结构:
#include "1.h"
...
struct test {
int a;
};
...
I have a 1.h wicth have the typedef using it:
我有一个 1.h wicth 使用它的 typedef:
...
typedef struct test testT;
...
Then I have a function that has a parameter in it depending on testT, wich is in 2.c:
然后我有一个函数,它有一个取决于 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 ... */
}
...
The 2.h file is like this:
2.h文件是这样的:
...
void funcTest(testT **t, int *size, ..);
...
I will pass a testT *varas the way below, at the main programam:
我将通过testT *var如下方式,在主程序中:
...
testT *varTest; int size;
funcTest(&varTest, &size);
...
The bizarre thing is that the code compile when I use struct testat 1.h file (removing struct testfrom 1.c - which is wrong). But, when running the compiled program, exactly where the error occurs is the place of t[0]->a.
奇怪的是,当我在 1.h 文件中使用struct test时,代码会编译(从 1.c 中删除struct test- 这是错误的)。但是,在运行编译后的程序时,错误发生的确切位置是t[0]->a 的位置。
I already tried 'everything' but nothing worked :( I have faith that is something very stupid, so if anybody knows something, please tell me :D Thanks!
我已经尝试了“一切”,但没有任何效果:(我相信这是非常愚蠢的事情,所以如果有人知道什么,请告诉我 :D 谢谢!
回答by sth
When you try to access the amember of the t[0]struct the compiler needs to know how this struct looks like (for example to see if there even is any amember in it). Since you didn't put the struct testtype definition anywhere where the compiler can see it when compiling 2.c, you get an error. The compiler doesn't know what a struct testcontains.
当您尝试访问结构体的a成员时,t[0]编译器需要知道该结构体的外观(例如,查看其中是否有任何a成员)。由于您没有将struct test类型定义放在编译器在编译时可以看到它的任何位置,因此2.c您会收到错误消息。编译器不知道 astruct test包含什么。
If you put the definition of the struct testin 1.h, the compiler sees how that type looks like and can use the struct members.
如果将struct testin的定义放入1.h,编译器会看到该类型的外观并可以使用结构成员。
Just put the complete type definition in 1.h, that's where it's supposed to be.
只需将完整的类型定义放在 中1.h,这就是它应该在的地方。
回答by Edwin Buck
Somewhere you have a preprocessed file that has
某处你有一个预处理文件
typedef struct test testT;
Which doesn't include
其中不包括
struct test {
int a;
};
Preprocessing inlines all the #includes directives. As long as you were only using a testT pointer, the compiler would have known to "allocate a pointer's worth of memory" and the compilation would have progressed further than expected.
预处理内联所有 #includes 指令。只要您只使用 testT 指针,编译器就会知道“分配一个指针的内存价值”,并且编译会比预期的进展得更远。
When you actually try to use that pointer to dereference something, the compiler would then realize it NEEDED the full definition of "struct test" and you would get the error displayed.
当您实际尝试使用该指针来取消引用某些内容时,编译器会意识到它需要“struct test”的完整定义,并且您会看到错误显示。
回答by Mat
If you want the struct to be usable both in 1.c and 2.c, it must be defined in a header file that is visible to both. I don't know why you say that this is "wrong", it's common practice and AFAIK there is no other way around that directly.
如果您希望结构在 1.c 和 2.c 中都可用,则必须在两者都可见的头文件中定义它。我不知道你为什么说这是“错误的”,这是常见的做法,AFAIK 没有其他直接解决方法。
If it's only defined in 1.c, then the compiler has no idea if struct testhas a member named "a" when processing 2.c.
如果只在1.c中定义,那么编译器在处理2.c时不知道是否struct test有名为“a”的成员。
Another option is to just keep the forward declaration as you have now, but also include accessor/mutator functions in the header. Then 2.c does not have to know about the "internals" of struct test, but can act on it. This is also very common in C APIs.
另一种选择是像现在一样保留前向声明,但也在头文件中包含访问器/修改器函数。那么 2.c 不必知道 的“内部” struct test,但可以对其采取行动。这在 C API 中也很常见。
(You could also define the struct identically both in 1.c and 2.c but that's a verybad idea.)
(您也可以在 1.c 和 2.c 中相同地定义结构,但这是一个非常糟糕的主意。)
回答by Vlad
The definition of struct Testis only visible inside the file 1.c. The code t[0]->adoesn't see that this struct has a member named a. The types shared between several compile units shuld be defined in a header!
的定义struct Test仅在文件 1.c 中可见。代码t[0]->a没有看到这个结构体有一个名为 的成员a。几个编译单元之间共享的类型应该在头文件中定义!
You should know that C/C++ compiles each .c file separately, so it has no way to know that the structure is defined in some other .c file.
您应该知道C/C++ 分别编译每个.c 文件,因此无法知道该结构是在其他.c 文件中定义的。
You should perhaps do the following:
您或许应该执行以下操作:
(1.h)
(1.h)
struct test {
int a;
};
...
(1.c)
(1.c)
#include "1.h"
...
(2.c)
(2.c)
#include "1.h"
...
void funcTest(testT **t, int *size, ..){
createMem(t,*size); /* void createMem(testT **t, int size); */
t[0]->a = 0;
/* ... more code ... */
}
回答by casablanca
But, when running the compiled program, exactly where the error occurs is the place of t[0]->a.
但是,在运行编译后的程序时,发生错误的正是t[0]->a的地方。
The pointer to the allocated memory is actually in *t, not t(as seen from your createMatrixcode at Pastebin), so you should really be doing:
指向已分配内存的指针实际上在*t,而不是t(从您createMatrix在 Pastebin的代码中可以看出),所以您真的应该这样做:
(*t)[0].a
and similarly in your forloop:
同样在你的for循环中:
(*matriz)[i].row

