In C/C++, the typedef
keyword allows us to set an alias for an existing type. Take this example:1
Three big applications of typedef
:
- Redefining structs or complicated types (i.e.,
long double
) to take a shorter name. - Redefining complicated pointers (i.e., double, triple pointers) to something simpler.
- Application- or implementation-specific alias for existing types (i.e.,
typedef int street_id
ortypedef vector<int> Stack
.
Note that this doesn’t make int
different from street_id
. We define an alias that we can use interchangeably. The compiler won’t care what the difference is!
Footnotes
-
From Beej’s Guide to C Programming. ↩