Module 7 · Structures, Unions, Enums and typedef — topic 4 of 4
typedef
typedef creates an alias for an existing type, making complex declarations readable and code easier to change. Learn typedef with structs, pointers, arrays and function pointers — and when not to use it.
Learning objectives
- Create type aliases with typedef
- Simplify struct and function-pointer declarations
- Recognise when typedef hides important information
Definition
A typedef declaration introduces a new name (an alias) for an existing type; it does not create a new distinct type.Introduction
Writing struct Student everywhere is wordy, and a declaration like int (*cmp)(const void *, const void *) is hard to read. typedef gives such types short, meaningful names.
The standard library uses typedef extensively: size_t, FILE, uint32_t and time_t are all typedef names that hide platform-specific details behind a portable name.
Why it matters
Readable type names make large programs easier to maintain, and changing the underlying type in one place updates the whole program.
Function-pointer callbacks and opaque library handles are almost always declared with typedef.
Detailed explanation
typedef existing_type NewName; — the syntax is a normal declaration with typedef in front; the name being declared becomes the alias.
typedef struct { ... } Student; lets you write Student s;. To refer to the type inside itself (e.g. a linked-list node), give the struct a tag: typedef struct Node { int v; struct Node *next; } Node;.
typedef int (*Comparator)(const void *, const void *); names a function-pointer type, so parameters can be declared as Comparator cmp.
typedef aliases are interchangeable with the original type — typedef int Meters; typedef int Seconds; does not stop you adding meters to seconds. For real type safety wrap values in distinct structs.
Avoid typedefs that hide pointers (typedef struct Node *NodePtr;) in most code: they make it unclear that a variable is a pointer that may be null or must be freed. Opaque library handles are the accepted exception.
Syntax
- A typedef name follows normal scope rules.
- By convention typedef names are PascalCase or end in
_t(though_tnames are reserved by POSIX). - typedef does not allocate memory or create a variable.
Code examples
Aliases for readability
3 vectors, sum (1.5, 1.0)
Self-referential struct with a tag
1 2 3
More examples
Function-pointer typedefs for a dispatch table
c2f 100 -> 212.00 km2mi 100 -> 62.14 mph unknown conversion
Step-by-step explanation
Converternames “pointer to a function taking and returningdouble”.Conversionpairs a name with a converter;TABLEis a constant lookup table.findsearches by name and returns a function pointer, orNULLif not found.- The caller checks for
NULLbefore callingf(100).
Expected output
Running “Function-pointer typedefs for a dispatch table” prints exactly:
Practical applications
FILE,size_t,pthread_tanduint8_tare typedefs in standard and POSIX headers.- Embedded code defines
typedef volatile uint32_t reg32_t;for hardware registers. - Plugin systems use typedef'd function pointers for callbacks.
Best practices
- Use typedef for structs used throughout a program and for function-pointer types.
- Don't hide pointers behind typedefs except for opaque handles.
- Keep typedefs in the header that defines the type.
Common mistakes
Using the typedef name inside its own definition
Avoid
Prefer
The alias does not exist until the typedef ends. Use a struct tag for self-reference.
Expecting typedef to create a distinct type
Avoid
Prefer
Aliases are interchangeable. Distinct structs make mixing units a compile error.
Important notes
- Names ending in
_tare reserved by POSIX for system types; many projects avoid them for their own typedefs. - C11 allows repeating an identical typedef, which C99 did not.
Key takeaways
typedefcreates an alias, not a new type.- Tag self-referential structs.
- Typedefs make function-pointer declarations readable.
Try it yourself
- 1Rewrite the Employee example from the structures lesson using
typedef struct. - 2Create
typedef int (*Predicate)(int);and writeint count_if(const int *a, int n, Predicate p).
Finished reading? Mark it complete to update your dashboard.
Suggested practice
3 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat does `typedef unsigned int uint;` do?
- ModerateWhy does this fail? `typedef struct { int v; Node *next; } Node;`
- AdvancedGiven `typedef int *IntPtr;`, what is the type of b in `IntPtr a, b;`?