We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Mutual Structs

Forward declarations can also be used when two structs reference each other (a circular reference). For example, a Person has a Computer and a Computer has a Person:

typedef struct Computer computer_t;
typedef struct Person person_t;

struct Person {
  char *name;
  computer_t *computer;
};

struct Computer {
  char *brand;
  person_t *owner;
};

Notice that the struct definitions end with just }; rather than } person_t;. Since we already created the typedef aliases in the forward declarations, we don't need to repeat them, though both styles are valid in C.

Note that when you use forward declarations, you must use pointers to incomplete types (Computer *computer;), not full values (Computer computer;). This is because the size of the struct is unknown.

Assignment

Complete the definitions of the Employee and Department structs. Take a look at the implementations in the .c file to understand how they should be defined.

Tip

A manager is just another employee_t.