|
Normally, a structure type needs to be defined before you use it. But sometimes you need to refer to a structure type before its definition. In that case, use a forward declaration of the type, which is just the type heading followed by a semicolon. For example,
struct Cell;
is a forward declaration for type Cell.
Forward declarations have a rather severe limitation. A forward declaration of type Cell allows you to use type Cell* before you define type Cell. But it does not allow you to use type Cell without a * before the definition of type Cell.
A definition of structure type T can refer to type T* without a forward declaration. For example, type definition
struct ListCell { ListCell* next; int item; };indicates that a value of type ListCell has two fields, one of which is a pointer to a ListCell. We will see that recursive structures are valuable for creating data structures.
Can a definition of structure type Kangaroo contain a field of type Kangaroo? Answer
What is the purpose of a forward declaration of a structure type? Answer
|