Structs & Data Structures · beginner · ~8 min

typedef

Give types nicer names.

Lesson

typedef creates an alias for an existing type. Most common use: drop the struct keyword from every site of use.

Don't abuse it — hiding pointer types behind a typedef (typedef foo_t *foo_handle) makes ownership/lifetime invisible to readers. Reserve typedef for value types and opaque handles.

Code examples

typedef struct point { double x, y; } point_t;
point_t p = {1.0, 2.0};

Common mistakes

  • Naming convention drift: some projects use _t suffix (point_t), others don't. Be consistent.
  • Typedef'ing a pointer (typedef foo *foo_p;) — usually a readability loss.