Solution to the compilation error: dereferencing pointer to incomplete type

Source: Internet
Author: User

When using C language to write a program, you may encounter an error:Dereferencing pointer to incomplete type. In fact, this error is because the struct type pointed to by the pointer is not defined.

There may be many causes, but there may be two possible causes:

1. When using the struct provided by library functions or kernels, it does not contain the corresponding header file. The solution is simple, that is, it is OK to include the corresponding header file.

2. If it is a self-defined struct, and the struct is exactly defined in the. c file. In this case, a similar error occurs when this struct pointer is used in other. c files.

 

In the second case, the author finds two solutions:

1. Place the struct definition in the. H file, and then include the struct in the. c file. This method is more common.

Note that when defining a header file, use a pre-compiled statement to enclose the header file. This prevents repeated inclusion of header files.

For example:

# Ifndef _ ABC_H

# Define _ ABC_H

Struct definition

# Endif

 

2. Define the struct from the previous one. c file copied to the error. in file c, because the struct definition only declares that the size of the struct is not actually allocated space, there is no problem in doing so.

Sample code:

A. c

# Include <stdio. h>
Struct obj_t {int x; int y ;};
Struct obj_t obj;
Struct obj_t * get_obj () {obj. x = 11; obj. y = 22;
Return & obj ;}
Void show (struct obj_t * p) {printf ("a = % d \ nb = % d \ n", p-> x, p-> y, _ func __);}

B .C

Void call_show ()

{Struct obj_t * p = get_obj ();
P-> x = 111; p-> y = 222;
Show (p );}

 

Main. c

Int main (int argc, char ** argv) {call_show ();}

Run the command: gcc a. c B. c main. c-o test

The following error occurs:

B. c: In function 'Call _ Show': B. c: 10: warning: initialization makesPointerFrom integer without a castB. c: 12: error:Dereferencing pointer to incomplete typeB. c: 13: error:Dereferencing pointer to incompleteTyd ;"

 

After B .C is modified as follows, the error disappears.

Struct obj_t {int x; int y ;};
Void call_show () {struct obj_t * p = get_obj ();
P-> x = 111; p-> y = 222;
Show (p );}

Solution to the compilation error: dereferencing pointer to incomplete type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.