C Typedef - Incomplete type

So, unexpectedly, the compiler decides to spit on his face: "The field client is of an incomplete type."

Here are the relevant code snippets:

customer.c

#include <stdlib.h> #include <string.h> #include "customer.h" struct CustomerStruct; typedef struct CustomerStruct { char id[8]; char name[30]; char surname[30]; char address[100]; } Customer ; /* Functions that deal with this struct here */ 

customer.h

Header file for customer.h

 #include <stdlib.h> #include <string.h> #ifndef CUSTOMER_H #define CUSTOMER_H typedef struct CustomerStruct Customer; /* Function prototypes here */ #endif 

This is where my problem is:

customer_list.c

 #include <stdlib.h> #include <string.h> #include "customer.h" #include "customer_list.h" #include "..\utils\utils.h" struct CustomerNodeStruct; typedef struct CustomerNodeStruct { Customer customer; /* Error Here*/ struct CustomerNodeStruct *next; }CustomerNode; struct CustomerListStruct; typedef struct CustomerListStruct { CustomerNode *first; CustomerNode *last; }CustomerList; /* Functions that deal with the CustomerList struct here */ 

This source file has a customer_list.h header file, but I don't think it matters.

My problem

In customer_list.c, in the line with the comment /* Error Here */ , the compiler complains about the field customer has incomplete type.

I have been working on this problem all day, and now I am going to pull out my eyeballs and mix them with strawberries.

What is the source of this error?

Thanks in advance:)

[PS if I forgot something, let me know. It was a busy day for me, as you might say]

+6
source share
4 answers

Move the structure declaration to the title:

 customer.h typedef struct CustomerStruct { ... } 
+11
source

In C, the compiler must be able to determine the size of any object that is directly referenced. The only way that sizeof(CustomerNode) can be calculated is with the Client definition that the compiler can use when creating customer_list.c.

The solution is to move the structure definition from customer.c to customer.h.

+6
source

You have an ad ahead of the Customer structure you are trying to create. This is not entirely acceptable, because the compiler has no idea about the structure of the structure, if it does not see its definition. So, you need to move the definition from the source file to the header.

+3
source

It seems like something

 typedef struct foo bar; 

will not work without a definition in the header. But something like

 typedef struct foo *baz; 

will work if you don't need to use baz->xxx in the header.

+1
source

All Articles