

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 3
click for more info
Not enough gems
Cost: 6 gems
1: Memory
incomplete
2: What Is an Address?
incomplete
3: Virtual Memory
incomplete
4: Pointers
incomplete
5: Why Pointers?
incomplete
6: Pointer Basics
incomplete
7: Pointers to Structs
incomplete
8: C Arrays
incomplete
9: Arrays As Pointers in C
incomplete
10: Multibyte Arrays
incomplete
11: Array Casting
incomplete
12: Pointer Size
incomplete
13: Arrays Decay to Pointers
incomplete
14: C Strings
incomplete
15: C String Library
incomplete
16: Forward Declaration
incomplete
17: Mutual Structs
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
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;
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.