typedef Specifier

Declares a name that, within its scope, becomes a synonym for a type.

typedef type-declaration synonym;

Remarks

You can use a typedef declaration to construct a shorter or more meaningful name for a type that is already defined by the language, or for a type that you have declared. A typedef can help encapsulate implementation details that may change over time.

In contrast to the class, struct, union, and enum declarations, typedef declarations do not introduce new types. Instead, they introduce new names for existing types.

Typedef names share the name space with ordinary identifiers. Therefore, a program can have a typedef name and also have a local-scope identifier that has the same name.

For more information, see these topics:

Example

// typedef_specifier1.cpp
typedef char FlagType;

int main()
{
}

void myproc( int )
{
    int FlagType;
}

When you declare a local-scope identifier by the same name as a typedef, or when you declare a member of a structure or union in the same scope or in an inner scope, you must specify the type specifier. For example:

typedef char FlagType;
const FlagType x;

To reuse the FlagType name for an identifier, a structure member, or a union member, the type must be provided:

const int FlagType;  // Type specifier required

It is insufficient to say

const FlagType;      // Incomplete specification

because the FlagType is taken to be part of the type, not an identifier that is being redeclared. This declaration is invalid, like

int;  // Invalid declaration 

You can declare any type by using typedef. This includes pointer, function, and array types. You can declare a typedef name for a pointer to a structure or union type before you define the structure or union type, as long as the definition has the same visibility as the declaration.

Examples

One use of typedef declarations is to make declarations more uniform and compact. For example:

typedef char CHAR;          // Character type.
typedef CHAR * PSTR;        // Pointer to a string (char *).
PSTR strchr( PSTR source, CHAR target );
typedef unsigned long ulong;
ulong ul;     // Equivalent to "unsigned long ul;"

To use typedef to specify fundamental and derived types in the same declaration, you can separate declarators by using commas. For example:

typedef char CHAR, *PSTR;

The following example provides the type DRAWF for a function that returns no value and takes two int arguments:

typedef void DRAWF( int, int );

After this typedef statement, the declaration

DRAWF box; 

would be equivalent to the declaration

void box( int, int );

typedef is often combined with struct to declare and name user-defined types:

// typedef_specifier2.cpp
#include <stdio.h>

typedef struct mystructtag
{
    int   i;
    double f;
} mystruct;

int main()
{
    mystruct ms;
    ms.i = 10;
    ms.f = 0.99;
    printf_s("%d   %f\n", ms.i, ms.f);
}

10 0.990000

See Also

Reference

C++ Keywords

C++ Type Names

Change History

Date

History

Reason

October 2009

Removed an inaccurate statement.

Customer feedback.