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:

    This type of man who is devoted to the study of wisdom is always most unlucky in everything, and particularly when it comes to procreating children; I imagine this is because Nature wants to ensure that the evils of wisdom shall not spread further throughout mankind.
    Desiderius Erasmus (c. 1466–1536)

    Poets are the hierophants of an unapprehended inspiration; the mirrors of the gigantic shadows which futurity casts upon the present; the words which express what they understand not; the trumpets which sing to battle and feel not what they inspire; the influence which is moved not, but moves. Poets are the unacknowledged legislators of the world.
    Percy Bysshe Shelley (1792–1822)