Error: In C, got the error “dereferencing pointer to incomplete type” in a struct pointer

痞子三分冷 提交于 2020-01-24 08:55:47

问题


Hello Everybody!

I got the following error, while trying to test a code for the game Clever Frog: error: dereferencing pointer to incomplete type

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.

I have a struct defined in a 1.c file:

#include "1.h"
...
struct test {
   int a;
   };
...

I have a 1.h wicth have the typedef using it:

...
typedef struct test testT;
...

Then I have a function that has a parameter in it depending on testT, wich is in 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:

...
void funcTest(testT **t, int *size, ..);
...

I will pass a testT *var as the way below, at the main programam:

...
testT *varTest; int size;

funcTest(&varTest, &size);
...

The bizarre thing is that the code compile when I use struct test at 1.h file (removing struct test from 1.c - which is wrong). But, when running the compiled program, exactly where the error occurs is the place of 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!


回答1:


When you try to access the a member of the t[0] struct the compiler needs to know how this struct looks like (for example to see if there even is any a member in it). Since you didn't put the struct test type definition anywhere where the compiler can see it when compiling 2.c, you get an error. The compiler doesn't know what a struct test contains.

If you put the definition of the struct test in 1.h, the compiler sees how that type looks like and can use the struct members.

Just put the complete type definition in 1.h, that's where it's supposed to be.




回答2:


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.

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.




回答3:


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.

If it's only defined in 1.c, then the compiler has no idea if struct test has a member named "a" when processing 2.c.

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.

(You could also define the struct identically both in 1.c and 2.c but that's a very bad idea.)




回答4:


The definition of struct Test is only visible inside the file 1.c. The code t[0]->a doesn't see that this struct has a member named a. The types shared between several compile units shuld be defined in a header!

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.

You should perhaps do the following:

(1.h)

struct test {
    int a;
};
...

(1.c)

#include "1.h"
...

(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 ... */
}



回答5:


But, when running the compiled program, exactly where the error occurs is the place of t[0]->a.

The pointer to the allocated memory is actually in *t, not t (as seen from your createMatrix code at Pastebin), so you should really be doing:

(*t)[0].a

and similarly in your for loop:

(*matriz)[i].row


来源:https://stackoverflow.com/questions/5275501/error-in-c-got-the-error-dereferencing-pointer-to-incomplete-type-in-a-struc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!