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

36,621

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

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

Solution 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.)

Solution 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 ... */
}
Share:
36,621
Shuryon
Author by

Shuryon

Updated on March 12, 2020

Comments

  • Shuryon
    Shuryon about 4 years

    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!