Opaque Types & Forward Declarations in C
An opaque type hides its layout: the header declares typedef struct Handle Handle; with no body, so users hold only pointers and can never see inside, sizeof it, or depend on its fields. All manipulation flows through your functions (Handle *create(void); void destroy(Handle *);) β true encapsulation in a language without private.
FILE* is the canonical opaque type. The pattern also breaks circular header dependencies (forward-declare instead of including) and shrinks rebuilds. Cost: heap allocation per object (no stack instances of incomplete types) and a function call per access β the standard price of hidden implementation.
Declare without defining
typedef struct Config Config; in the header β a complete type name with deliberately missing layout.
Pointers only outside
Users declare Config *c; and pass it around β sizeof, member access, and stack instances all correctly rejected.
Define once, privately
The .c file holds the full struct definition plus create/destroy/access functions β the single place layout exists.
Break header cycles
Mutually referencing structs forward-declare each other instead of including in circles β compile-time deadlock resolved.
// counter.h: typedef struct Counter Counter;// Counter *counter_new(void);// void counter_bump(Counter *c);// int counter_get(const Counter *c);// counter.c defines struct Counter { int n; }; plus bodies.// main.c: Counter *c = counter_new(); counter_bump(c);Recommended resourcesAffiliate links Β· may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C yourself β building beats watching.