typedef definitions

A typedef declaration lets you define your own identifiers that can be used in place of type specifiers such as int, float, and double. A typedef declaration does not reserve storage. The names you define using typedef are not new data types, but synonyms for the data types or combinations of data types they represent.

The namespace for a typedef name is the same as other identifiers. The exception to this rule is if the typedef name specifies a variably modified type. In this case, it has block scope.

When an object is defined using a typedef identifier, the properties of the defined object are exactly the same as if the object were defined by explicitly listing the data type associated with the identifier.

IBM extension

typedef definitions are extended to handle vector types, provided that the VMX support is enabled. A vector type can be used in a typedef definition, and the new type name can be used in the usual ways, except for declaring other vectors. In a vector declaration context, a typedef name is disallowed as a type specifier. The following example illustrates a typical usage of typedef with vector types:

typedef vector unsigned short vint16;
vint16 v1;
End of IBM extension

Related information

Examples of typedef definitions

The following statements define LENGTH as a synonym for int and then use this typedef to declare length, width, and height as integer variables:

typedef int LENGTH;
LENGTH length, width, height;

The following declarations are equivalent to the above declaration:

int length, width, height;

Similarly, typedef can be used to define a structure, union, or C++ class. For example:

typedef struct {
                int scruples;
                int drams;
                int grains;
               } WEIGHT;

The structure WEIGHT can then be used in the following declarations:

WEIGHT  chicken, cow, horse, whale;

In the following example, the type of yds is "pointer to function with no parameter specified, returning int".

typedef int SCROLL();
extern SCROLL *yds; 

In the following typedefs, the token struct is part of the type name: the type of ex1 is struct a; the type of ex2 is struct b.

typedef struct a { char x; } ex1, *ptr1;
typedef struct b { char x; } ex2, *ptr2;  

Type ex1 is compatible with the type struct a and the type of the object pointed to by ptr1. Type ex1 is not compatible with char, ex2, or struct b.

C++ only

In C++, a typedef name must be different from any class type name declared within the same scope. If the typedef name is the same as a class type name, it can only be so if that typedef is a synonym of the class name. This condition is not the same as in C. The following can be found in standard C headers:

typedef class C { /*  data and behavior  */ } C;

A C++ class defined in a typedef without being named is given a dummy name and the typedef name for linkage. Such a class cannot have constructors or destructors. For example:

typedef class {
              } Trees;

Here the function Trees() is an ordinary member function of a class whose type name is unspecified. In the above example, Trees is an alias for the unnamed class, not the class type name itself, so Trees() cannot be a constructor for that class.

End of C++ only