liveBook · Manning

Typedef

As explained in Modern C

A typedef is a C declaration that introduces an alias for an existing type.

Consider a structure whose tag is awkward to repeat: typedef struct birdStruct birdStruct; struct birdStruct { ... }; The first declaration gives struct birdStruct the shorter name birdStruct. A typedef introduces an alias for an existing C type; it does not create a distinct type. section 6.4

Use typedef for several kinds of existing types:

  • Give a structure a convenient name, including through a forward declaration: typedef struct toto toto; then use toto* in an interface. section 6.4 section 11.2
  • Alias arrays and compose aliases: typedef double matrix[16][64]; makes matrix B and double C[16][64] the same type. section 6.4
  • Remember that names ending in _t are reserved; the C standard uses this convention for typedef names such as size_t. section 6.4

Keep pointers visible

typedef struct toto toto; toto* toto_get(void); void toto_destroy(toto*); void toto_doit(toto*, unsigned); This shortens the structure spelling without hiding that the interface accepts pointers. Hiding the pointer itself inside typedef toto is discouraged because it conceals that a null pointer may be meaningful. section 11.2

TAKEAWAY: typedef supplies another name, not another entry in C’s type system. It resembles a naming macro, but the alias participates as a type declaration. section 6.0 section 6.4

How Modern C covers it

How deep it goes
Defines alias semantics and demonstrates structures, arrays, forward declarations, and pointer-visibility tradeoffs.
Examples
Uses same-name structure aliases, opaque structure interfaces, and nested array aliases as concrete declaration patterns.
What it assumes
Assumes basic C declarations and structures while deferring full pointer details to later treatment.
What you can do after
Apply it to shorten declarations while preserving the underlying type and keeping important pointer syntax visible.
How it connects
Connects structures, arrays, pointers, macros, forward declarations, and the standard type size_t.

Also explained in

Compare how 2 books explain typedef →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.