Typedef - Using Typedef With Type Casts

Using Typedef With Type Casts

A typedef is created using type declaration syntax but can be used as if it were created using type cast syntax. (Type casting changes a data type.) For instance, in each line after the first line of:

typedef int (*funcptr)(double); // pointer to function of double returning int funcptr x = (funcptr) NULL; // C or C++ funcptr y = funcptr(NULL); // C or C++ funcptr z = static_cast(NULL); // C++ only

funcptr is used on the left-hand side to declare a variable and is used on the right-hand side to cast a value. Thus, typedefs can be used by programmers who do not wish to figure out how to convert declaration syntax to type cast syntax.

Note that, without the typedef, it is generally not possible to use declaration syntax and cast syntax interchangeably. For example:

void *p = NULL; int (*x)(double) = (int (*)(double)) p; // This is legal int (*)(double) y = (int (*)(double)) p; // Left-hand side is not legal int (*z)(double) = (int (*p)(double)); // Right-hand side is not legal

Read more about this topic:  Typedef

Famous quotes containing the words type and/or casts:

    Mediocre people have an answer for everything and are astonished at nothing. They always want to have the air of knowing better than you what you are going to tell them; when, in their turn, they begin to speak, they repeat to you with the greatest confidence, as if dealing with their own property, the things that they have heard you say yourself at some other place.... A capable and superior look is the natural accompaniment of this type of character.
    Eugène Delacroix (1798–1863)

    The desire to be significant casts a pall.
    Mason Cooley (b. 1927)