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 , such as in size_t and time_t. This is practiced in other coding systems, although POSIX explicitly reserves this practice for POSIX data types.
Examples
This creates the type as a synonym of the type :typedef int length;
Documentation use
A typedef declaration may be used as documentation by indicating the meaning of a variable within the programming context, e.g., it may include the expression of a unit of measurement or counts. The generic declarations,
int current_speed; int high_score;
void congratulate(int your_score) {
if (your_score > high_score) {
// ... } }
may be expressed by declaring context specific types:
typedef int km_per_hour; typedef int points;
// `km_per_hour` is synonymous with `int` here, and thus, the compiler treats
// our new variables as integers. km_per_hour current_speed; points high_score;
void congratulate(points your_score) {
if (your_score > high_score) {
// ... } }
Both sections of code execute identically. However, the use of typedef declarations in the second code block makes it clear that the two variables, while representing the same data type , store different or incompatible data. The definition in of indicates to the programmer that (or any other variable not declared as a ) should not be passed as an argument. This would not be as apparent if both were declared as variables of datatype. However, the indication is for the programmer only; the C/C++ compiler considers both variables to be of type and does not flag type mismatch warnings or errors for "wrong" argument types for in the code snippet below:
void foo() {
km_per_hour km100 = 100; congratulate(km100); }
Type simplification
A typedef may be used to simplify the declaration of a compound type (struct, union) or pointer type. For example,
struct MyStruct {
int data1; char data2; };
This defines the data type . A variable declaration of this type in C also requires the keyword , but it may be omitted in C++:
struct MyStruct a;
A typedef declaration eliminates the requirement of specifying in C. For example, the declaration
typedef struct MyStruct newtype;
is reduced to:
newtype a;
The structure declaration and typedef may also be combined into a single statement:
typedef struct MyStruct {
int data1; char data2; } newtype;
Or it may be used as follows:
typedef struct {
int data1; char data2; } newtype;
In C++, in contrast to C, the keywords , , and are optional in variable declarations that are separate from the definitions, as long as there is no ambiguity to another identifier:
struct MyStruct x; MyStruct y;
As such, can be used wherever can be used. However, the reverse is not true; for instance, the constructor methods for cannot be named .
A notorious example where even C++ needs the keyword is the POSIX stat system call that uses a struct of the same name in its arguments:
int stat(const char *filename, struct stat *buf) { // ... }
Here both C as well as C++ need the keyword in the parameter definition.
Pointers
The typedef may be used to define a new pointer type.
typedef int *intptr;
intptr ptr;
// Same as: // int *ptr;
is a new alias with the pointer type . The definition, , defines a variable with the type . So, is a pointer which can point to a variable of type .
Using typedef to define a new pointer type may sometimes lead to confusion. For example:
typedef int *intptr;
// Both 'cliff' and 'allen' are of type int*. intptr cliff, allen;