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

Forward Declaration

Sometimes you have a struct that may need to reference itself, or be used recursively.

For example, consider a Node struct that can contain other Nodes. This might be useful for building a linked list or a tree:

typedef struct Node {
  int value;
  node_t *next;
} node_t;

The problem here is that node_t is not defined yet, so the compiler will complain. To fix this, we can add a forward declaration. A forward declaration lets the compiler know about the existence of a struct type before it's fully defined:

typedef struct Node node_t;

typedef struct Node {
  int value;
  node_t *next;
} node_t;

Note that the forward declaration must match the eventual definition, so you can't do something like this:

typedef struct Node node_t;

typedef struct BadName {
  int value;
  node_t *next;
} node_t;

Assignment

Sneklang, like Python, is built on the idea of dynamic objects, and objects need to be able to store other objects.

Run the code in its current state. Notice that the .h file is producing an error because the Object struct references itself. Fix it with a forward declaration.