typedef is a reserved keyword in the programming languages C, C++, and Objective-C. It is used to create an additional name (alias) for another data type, but does not create a new type, except in the obscure case of a qualified typedef of an array type where the typedef qualifiers are transferred to the array element type. As such, it is often used to simplify the syntax of declaring complex data structures consisting of struct and union types, although it is also commonly used to provide specific descriptive type names for integer data types of varying sizes.
Syntax A typedef declaration follows the same syntax as declaring any other C identifier. The keyword typedef itself is a specifier which means that while it typically appears at the start of the declaration, it can also appear after the type specifiers or between two of them. In the C standard library and in POSIX specifications, the identifier for the typedef definition is often suffixed with _t, such as in size_t and time_t. This is practiced in some other coding systems, although POSIX explicitly reserves this practice for POSIX data types. In C++, this _t suffix is not used for typedefs (as C++ does not mandate prefixing structs with the struct keyword), but does appear in some types in the C++ Standard Library (especially for tag types, like std::in_place_t) along with other suffixes like _v (typically representing an abbreviation for a T::value field).
Examples This snippet declares the type Length as an alias of the type int, allowing a user to use Length as a type in declarations where they would otherwise use int:
Documentation use A typedef declaration may be used to give more contextual meaning than is given by the underlying type, hinting to the user of functions or variables a certain meaning or format is expected. For example, in the code below, the type int is being further clarified to the distinct aliases Points and KmPerHour:
The use of typedef here is used to indicate that only variables declared as Points (or variables that can be interpreted as a Points type) should be used as the first argument, and variables declared as KmPerHour for the second. The value of using typedef in this manner would be for specific cases where masking the underlying type with an alias would add substantial clarity for the programmer, and not for any sort of type enforcement from the compiler, since typedefs are translated back to underlying types before any error checking occurs. For example, this code will compile, without any error:
Declaration simplification A typedef may be used to simplify the declarations of objects having types with verbose names, such as struct, union, or enum types. The point of this is to make it more convenient to declare variables with certain types. For example,
Along with providing previously declared types directly after the typedef keyword, one may also put a full definition of a struct, union, or enum. This has the effect of providing both a type definition and an alias in one statement.
When a definition is provided in the typedef declaration, the tag (i.e. name of the underlying type) is optional, leaving only the alias. Modifying the previous snippet:
In C++, unlike in C, the tag of a class, struct, union, or enum type is called a class name, and can be used as a type alias automatically without any typedef declaration.
The correspondence between this C++ feature and typedef is very strong, extending to the fact that it is possible to shadow the simple type name in a nested scope by declaring it as the identifier of another kind of entity. In such a case, the C-style full type name (an "elaborated type specifier") can still be used to refer to the class or enum type. In C++, then, MyStruct can be used anywhere that MyNewType can be used, as long as there are no other declarations of these identifiers. The reverse is not true, however, for C++ requires the class name for some purposes, such as the names of constructor methods. A notorious example where even C++ needs the struct keyword is the POSIX stat system call that uses a struct of the same name in its arguments:
Here both C as well as C++ need the struct keyword in the parameter definition.
Pointers The typedef may be used to define a new pointer type.
IntPtr is a new alias with the pointer type int*. The definition, IntPtr ptr;, defines a variable ptr with the type int*. So, ptr is a pointer which can point to a variable of type int. Using typedef to define a new pointer type may sometimes lead to confusion. For example:
Above, IntPtr cliff, allen; means defining 2 variables with int* type for both. This is because a type defined by typedef is a type, not an expansion. In other words, IntPtr, which is the int* type, decorates both cliff and allen. For IntPtr cliff2, *allen2;, the IntPtr type decorates the cliff2 and *allen2. So, IntPtr cliff2, *allen2; is equivalent to 2 separate definitions, IntPtr cliff2; and IntPtr* allen2. IntPtr* allen2 means that allen2 is a pointer pointing to a memory with int* type. Shortly, allen2 has the type, int**.
Constant pointers Again, because typedef defines a type, not an expansion, declarations that use the const qualifier can yield unexpected or unintuitive results. The following example declares a constant pointer to an integer type, not a pointer to a constant integer:
Since it is a constant pointer, it must be initialized in the declaration.
Structures and structure pointers Typedefs can also simplify definitions or declarations for structure pointer types. Consider this:
Using typedef, the above code can be rewritten like this:
In C, one can declare multiple variables of the same type in a single statement, even mixing structure with pointer or non-pointers. However, one would need to prefix an asterisk to each variable to designate it as a pointer. In the following, a programmer might assume that errptr was indeed a Node*, but a typographical error means that errptr is a Node. This can lead to subtle syntax errors.
By defining the typedef Node*, it is assured that all variables are structure pointer types, or say, that each variable is a pointer type pointing to a structure type.
Function pointers
The preceding code may be rewritten with typedef specifications:
… excerpt ends here. Continue reading the full article.