类型强制转换的转换Type-Cast Conversions

可以使用类型强制转换来显式转换类型。You can use type casts to explicitly convert types.

语法Syntax

cast-expressioncast-expression:
unary expressionunary expression

( type-name ) cast-expression( type-name ) cast-expression

type-nametype-name:
specifier-qualifier-list abstract-declarator optspecifier-qualifier-list abstract-declarator opt

type-name 是类型,cast-expression 是要转换为该类型的值。The type-name is a type and cast-expression is a value to be converted to that type. 具有类型强制转换的表达式不是左值。An expression with a type cast is not an l-value. cast-expression 也会被转换,就好像它已分配到 type-name 类型的变量一样。The cast-expression is converted as though it had been assigned to a variable of type type-name. 赋值的转换规则(在赋值转换中进行了概述)也适用于类型强制转换。The conversion rules for assignments (outlined in Assignment Conversions) apply to type casts as well. 下表显示了可强制转换为任何给定类型的类型。The following table shows the types that can be cast to any given type.

目标类型Destination Types 可能的源Potential Sources
整型Integral types 整数类型或浮点类型,或者指向对象的指针Any integer type or floating-point type, or pointer to an object
浮点Floating-point 任何算术类型Any arithmetic type
指向对象的指针或 (void \)A pointer to an object, or (*void ***) 任何整数类型、(void \)、指向对象的指针或函数指针Any integer type, (*void ***), a pointer to an object, or a function pointer
函数指针Function pointer 任何整数类型、指向对象的指针或函数指针Any integral type, a pointer to an object, or a function pointer
结构、联合或数组A structure, union, or array None
Void 类型Void type 任何类型Any type

任何标识符均可强制转换为 void 类型。Any identifier can be cast to void type. 但是,如果 type-cast 表达式中指定的类型不是 void,则要强制转换为该类型的标识符不能是 void 表达式。However, if the type specified in a type-cast expression is not void, then the identifier being cast to that type cannot be a void expression. 任何表达式均可转换为 void,但 void 类型的表达式不能强制转换为其他类型。Any expression can be cast to void, but an expression of type void cannot be cast to any other type. 例如,带有 void 返回类型的函数不能将其返回值强制转换为另一类型。For example, a function with void return type cannot have its return cast to another type.

请注意,void \* 表达式具有指向 void 的类型指针,而不具有类型 voidNote that a void \* expression has a type pointer to void, not type void. 如果对象强制转换为 void 类型,则生成的表达式不能分配给任何项。If an object is cast to void type, the resulting expression cannot be assigned to any item. 同样,type-cast 对象是不可接受的左值,因此不能对 type-cast 对象进行任何分配。Similarly, a type-cast object is not an acceptable l-value, so no assignment can be made to a type-cast object.

Microsoft 专用Microsoft Specific

只要标识符的大小不变,类型强制转换就可以是左值表达式。A type cast can be an l-value expression as long as the size of the identifier does not change. 有关左值表达式的信息,请参阅左值和右值表达式For information on l-value expressions, see L-Value and R-Value Expressions.

结束 Microsoft 专用END Microsoft Specific

可以使用强制转换将表达式转换为类型 void,但生成的表达式仅能用于不需要值的位置。You can convert an expression to type void with a cast, but the resulting expression can be used only where a value is not required. 转换为 void \* 再转换回原始类型的对象指针将返回到其原始值。An object pointer converted to void \* and back to the original type will return to its original value.

另请参阅See Also

类型转换Type Conversions