C++ Type Names

Synonyms for both fundamental and derived types can be defined using the typedef keyword. The following code illustrates the use of typedef:

typedef unsigned char BYTE;   // 8-bit unsigned entity.
typedef BYTE *        PBYTE;  // Pointer to BYTE.

BYTE Ch;                      // Declare a variable of type BYTE.
PBYTE pbCh;                   // Declare a pointer to a BYTE
                              //  variable.

The preceding example shows uniform declaration syntax for the fundamental type unsigned char and its derivative type unsigned char *. The typedef construct is also helpful in simplifying declarations. A typedef declaration defines a synonym, not a new, independent type. The following example declares a type name (PVFN) representing a pointer to a function that returns type void. The advantage of this declaration is that, later in the program, an array of these pointers is declared very simply.

// type_names.cpp
// Prototype two functions.
void func1(){};
void func2(){};

//  Define PVFN to represent a pointer to a function that
//   returns type void.
typedef void (*PVFN)();

int main()
{
   // Declare an array of pointers to functions.
   PVFN pvfn[] = { func1, func2 };
   
   // Invoke one of the functions.
   (*pvfn[1])();
}

See Also

Reference

Types (C++)