Forward declarations in headers
What you'll learn: How to speed up your builds and avoid circular dependencies by telling the compiler a type exists without including its entire file.
The problem with "Include Everything"
Imagine you are writing a recipe for a cake. To explain how to make the frosting, you don't need to paste the entire 50-page "History of Sugar" book into your recipe. You just need to know that "Sugar" is a thing that exists.
In C, we often use #include to bring in header files. However, if player.h includes enemy.h, and enemy.h includes player.h, the compiler gets stuck in an infinite loop (a circular dependency). Even if you avoid loops, including heavy headers everywhere makes the compiler do extra work, slowing down your build times.
The "Heads Up" approach
A forward declaration is a way of saying to the compiler: "Hey, there is a struct named Enemy coming later. I’m not telling you what’s inside it yet, but I want you to know the name exists so I can use pointers to it."
Because all pointers are the same size in memory, the compiler doesn't need to know the internal details of a struct to handle a pointer to it.
Putting it into practice
Instead of using #include "enemy.h" inside your player.h file, you can simply declare the struct at the top.
/* player.h */
#ifndef PLAYER_H
#define PLAYER_H
// Forward declaration: "A struct named Enemy exists"
struct Enemy;
typedef struct {
int health;
// We can use a pointer to Enemy because the compiler
// knows 'struct Enemy' is a valid type name.
struct Enemy* target;
} Player;
void attack_enemy(Player* p, struct Enemy* target);
#endif
By doing this, player.h no longer needs to know everything about enemy.h. It only cares about the address of an enemy. You only need to perform the actual #include "enemy.h" inside your .c source file, where you finally need to access the enemy's specific fields (like target->health).
Why this matters
Forward declarations are a professional "clean code" habit. They decouple your files, meaning a small change in one header won't trigger a massive recompilation of your entire project. It keeps your code modular, lean, and fast.
Key takeaway
Use forward declarations for pointers to structs in your headers to prevent circular dependencies and reduce compile times.