typedef definitions

Note: IBM supports selected features of C11, known as C1X before its ratification. IBM will continue to develop and implement the features of this standard. The implementation of the language level is based on IBM's interpretation of the standard. Until IBM's implementation of all the C11 features is complete, including the support of a new C11 standard library, the implementation may change from release to release. IBM makes no attempt to maintain compatibility, in source, binary, or listings and other compiler interfaces, with earlier releases of IBM's implementation of the C11 features.

You can use thetypedef declaration to 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 name space for a typedef name is the same as other identifiers. 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.

C11

Using typedef redeclaration, you can redefine a name that is a previous typedef name in the same scope to refer to the same type if the type is not a variably modified type. For example:

typedef char AChar;
typedef char AChar;

IBM extensionWhen any extended language level is in effect, typedef redeclaration supports all types, including a variably modified type.End IBM extension

For more information about variably modified types, see Variable length arrays.

C11

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 preceding declarations are equivalent to the following 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 parameters, returning int".
typedef int SCROLL(void);
extern SCROLL *yds; 
In the following typedef definitions, 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.

Begin 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.

A C++ class defined in a typedef definition without being named is given a dummy name. Such a class cannot have constructors or destructors. Consider the following example:
typedef class {
               ~Trees();
              } Trees;
In this example, an unnamed class is defined in a typedef definition. Trees is an alias for the unnamed class, but not the class type name. So you cannot define a destructor ~Trees() for this unnamed class; otherwise, the compiler issues an error.
End C++ only
Begin C++11 only

Declaring typedef names as friends

In the C++11 standard, the extended friend declarations feature is introduced, with which you can declare typedef names as friends. For more information, see Extended friend declarations.

End C++11