Tag Archives: dereferencing pointer to incomplete type

[Solved] Compilation error: dereferencing pointer to incomplete type…

Today my colleague asked me a question. He reported an error when he made “Line 201 : dereferencing pointer to incomplete type”. I immediately checked a lot of information, but I didn’t see why. Finally, the problem was solved, and the principle was understood, so I recorded it.

His question is specifically like this.

1
2
3
4
5
6
#include <netinet/ip_icmp.h>
...
struct icmp* aaa;
    aaa = (struct icmp*)malloc(sizeof(struct icmp)); 行;
    aaa->icmp_type=1;
...

 

Error on line 201 during make: dereferencing pointer to incomplete type.

First of all, let me talk about the meaning of this error. In layman’s terms, I tried to access the variable pointed to by the pointer, but found that the variable was an incomplete type, and more errors occurred in accessing the members of the structure union. It can be seen from the code that the variable pointed to by icmp_type is actually accessed from line 201, and line 200 has not been accessed yet.

So I guessed, is struct icmp not defined? Then I checked the /usr/include/netinet/ip_icmp.h file and found that there is a definition of struct icmp. It’s weird, isn’t it? After writing some demo tests, the final conclusion is that there is really no definition of struct icmp!

It’s even stranger to see this. Why is this conclusion? Looking closely at the /usr/include/netinet/ip_icmp.h file, you will find that the definition of struct icmp is contained in a macro j, as shown below:

1
2
3
4
5
6
7
8
9
...
#ifdef __USE_BSD
...
struct icmp {
...
}
...
#endif /*END OF ifdef __USE_BSD*/
...

 

When you see this, you should understand. When compiling, if -D__USE_BSD is not added to the compile command gcc …, then the definition of struct icmp will not be included, so the previous line 201 error is reported: Dereferencing pointer to incomplete type, and that’s how it led me to start wondering why it is clearly defined, but it is said to be an incomplete type. So in order to verify this conclusion, I wrote a small demo to test, and found that the compiler will pass if -D__USE_BSD is added, otherwise it will fail to compile.

In the process of solving this problem, I wrote a lot of demos, let’s summarize below.

1. If the error “dereferencing pointer to incomplete type ” is reported , first try to find out whether the definition of the structure variable in the line can be found, you can use the grep “struct xxx” /usr/include -R command to recursively search the /usr/include directory If found, you can #include in the .c file. If it is a non-standard header file, add the -I header file directory to the compile command, for example (-I/usr/local/xxx/include).

2. If the error “dereferencing pointer to incomplete type ” is still reported after #include , try to check the file carefully to see if the definition of the structure is wrapped by a compiled macro. If it is indeed in the package of a compiled macro, Add -D compilation macro in the compilation command (such as -D__USE_BSD)

After the above two steps, the error of “dereferencing pointer to incomplete type ” can basically be solved