Section 11.2. Type Names


11.2. Type Names

To convert a value explicitly from one type to another using the cast operator, you must specify the new type by name. For example, in the cast expression (char *)ptr, the type name is char * (read: "char pointer" or "pointer to char"). When you use a type name as the operand of sizeof, it appears the same way, in parentheses. Function prototype declarations also designate a function's parameters by their type names , even if the parameters themselves have no names.

The syntax of a type name is like that of an object or function declaration, but with no identifier (and no storage class specifier). Two simple examples to start with:

     unsigned char

The type unsigned char.

     unsigned char *

The type "pointer to unsigned char."

In the examples that follow, the type names are more complex. Each type name contains at least one asterisk (*) for "pointer to," as well as parentheses or brackets. To interpret a complex type name, start with the first pair of brackets or parentheses that you find to the right of the last asterisk. (If you were parsing a declarator with an identifier rather than a type name, the identifier would be immediately to the left of those brackets or parentheses.) If the type name includes a function type, then the parameter declarations must be interpreted separately.


float *[ ]

The type "array of pointers to float." The number of elements in the array is undetermined.


float (*)[10]

The type "pointer to an array of ten elements whose type is float."


double *(double *)

The type "function whose only parameter has the type pointer to double, and which also returns a pointer to double."


double (*)( )

The type "pointer to a function whose return value has the type double." The number and types of the function's parameters are not specified.


int *(*(*)[10])(void)

The type "pointer to an array of ten elements whose type is pointer to a function with no parameters which returns a pointer to int."



C(c) In a Nutshell
C in a Nutshell (In a Nutshell (OReilly))
ISBN: 0596006977
EAN: 2147483647
Year: 2006
Pages: 473

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net