Typedef Aliases
beginner11 min readLesson 55 of 148
typedef gives a type a shorter, intention-revealing name.
The basic form
typedef unsigned long ulong; // 'ulong' now names unsigned long
typedef int Score; // Score is int, with intent
typedef does not create a new type — it creates a new NAME for an existing
type. Score and int are interchangeable.
The classic: typedef struct
typedef struct {
int x;
int y;
} Point; // now 'Point', not 'struct Point'
Point p = {1, 2}; // no struct keyword needed
Or naming a tagged struct so it can self-reference:
typedef struct Node Node;
struct Node {
int value;
Node *next; // self-reference needs the tag
};
You will build this Node in module 20.
When to alias
- burying
unsigned long longbehindu64 typedef struct {...} Config;— the dominant C idiom- naming semantic roles:
typedef int UserId;