Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

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])();
}

Community Additions

ADD
Show:
© 2015 Microsoft