Search code examples
cpointersheaderstructdereference

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


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!


Solution

  • 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.