Why should we typedef a struct so often in C?
c-programmingtypedefstructcoding-style
Abstraction: Rationale and tradeoffs of typedef-ing structs in C
Key points:
- Typedef-ing a struct eliminates the need to write
structeverywhere, improving readability and enabling opaque-type patterns (used widely by GTK+). - Without a typedef, a typo in a struct tag silently creates a new empty type instead of a compiler error; typedef forces the error to be caught.
- Linux kernel style explicitly discourages struct typedefs because they hide whether a variable is a pointer or value and pollute the global namespace.
- Legitimate uses per kernel guidelines: truly opaque objects, portable integer-width aliases (u8/u16/u32), and sparse type-checking.
- Typedef-only structs (no tag name) cannot be forward-declared, forcing full inclusion of the header everywhere the type is used.
- C11 allows type redefinitions, removing one older argument against typedef; C++ classes already implicitly typedef their tag names.
Connections: Stackoverflow · Typedef · C Type System
Source: http://stackoverflow.com/questions/252780/why-should-we-typedef-a-struct-so-often-in-c