Tag Archives: “dereferencing pointer to incomplete type…

About the compilation error “dereferencing pointer to incomplete type

Today, my colleague asked me a question. When he made, he reported an error, “line 201: dereferencing pointer to incomplete type”. I immediately consulted a lot of materials, but I didn’t see a reason. Finally, the problem was solved and the principle was understood, so I recorded it

This is his problem

?

one

two

three

four

five

six

#include < netinet/ip_icmp.h>

...

struct

icmp* aaa;

    

aaa = (

struct

icmp*)

malloc

(

sizeof

(

struct

icmp));

// assume line 200

    

aaa-> icmp_type=1;

// assume line 201

...

 

When making, an error is reported in line 201: dereferencing pointer to incomplete type

First, let’s talk about the meaning of error reporting. Generally speaking, when you try to access the variable pointed to by the pointer, you find that the variable is an incomplete type, and most of the errors are in accessing the members of the structure consortium. It can be seen from the code that the ICMP is actually accessed from line 201_The variable pointed to by type has not been accessed in line 200

So I wonder if struct ICMP is not defined?So I looked at/usr/include/netinet/IP roughly_ICMP. H file, found the definition of struct ICMP. It’s strange, isn’t it?After writing some demo tests, the final conclusion is that there is no definition of struct ICMP

It’s even more strange to see here. Why is this conclusion?Take a closer look at/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:

?

one

two

three

four

five

six

seven

eight

nine

...

#ifdef __USE_BSD

...

struct

icmp {

...

}

...

#endif /*END OF ifdef __USE_BSD*/

...

 

It should be clear from here that during compilation, if the compilation command GCC… Is not added with – D__USE_For BSD, the definition of struct ICMP will not be included, so an error is reported on line 201: dereferencing pointer to include type. That is why I always wondered why it was an incomplete type when there was a definition. So in order to verify this conclusion, I wrote a small demo to test and found that plus – D__USE_BSD will compile, otherwise it will not compile

In the process of solving this problem, I have written many demos, which are summarized below

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

2. If #include still reports the error “dereferencing pointer to include type”, try to check the file carefully to see if the definition of the structure is wrapped by a compilation macro. If it is indeed wrapped by a compilation macro, add – D compilation macro (such as – D) to the compilation command__USE_BSD)

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

Transferred from: http://my.oschina.net/michaelyuanyuan/blog/68203?fromerr=quiozj2B