A
struct or
union declaration that does not identify any internal fields is an
incomplete type. The compiler knows the name of the incomplete type, but it does not know the size of the incomplete type; thus, you cannot declare variables of that type (including arrays of that type) nor can an incomplete type be passed to
sizeof. You can, however, declare pointers to an incomplete type. Declaration of an incomplete type is frequently followed by a redeclaration of that type that identifies the internal fields. This completes the type.
What good is an incomplete type?
It is helpful for declaring linked lists. The following snippet could not declare the link without the incomplete type declaration.
- struct node;
-
struct node { struct node *link; int payload };
It is helpful for information hiding. Users of a service include the public header and then call the public functions. The implementation of the service includes the private header. This leaves users of the service unable to access individual fields of the structure.
- Public header...
-
struct myhandle;
-
struct myhandle *getNewThing(void);
-
int changeThing(struct myhandle *);
-
-
Private header...
-
#include "public header"
-
struct myhandle {
-
...
-
};