In C/C++, the typedef keyword allows us to set an alias for an existing type. Take this example:1

//     original name  new name
//            |         |
//            v         v
//      |-----------| |----|
typedef struct animal animal;

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 or typedef 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

  1. From Beej’s Guide to C Programming.