指针类型之间的转换Conversions to and from Pointer Types

指向值的一个类型的指针可以转换为指向另一类型的指针。A pointer to one type of value can be converted to a pointer to a different type. 但是,由于对齐需求和存储中不同类型的大小,结果可能是未定义的。However, the result may be undefined because of the alignment requirements and sizes of different types in storage. 指向对象的指针可转换为指向其类型要求小于或等于严格存储对齐的对象的指针,然后再次返回而不做更改。A pointer to an object can be converted to a pointer to an object whose type requires less or equally strict storage alignment, and back again without change.

指向 void 的指针可转换为/自指向任何类型的指针,且不受限制或不丢失信息。A pointer to void can be converted to or from a pointer to any type, without restriction or loss of information. 如果结果转换回原始类型,则将恢复原始指针。If the result is converted back to the original type, the original pointer is recovered.

如果指针转换为另一个类型相同但具有不同的或其它限定符的指针,则新指针与旧指针相同(新限定符强加的限制除外)。If a pointer is converted to another pointer with the same type but having different or additional qualifiers, the new pointer is the same as the old except for restrictions imposed by the new qualifier.

指针值也可以转换为整数值。A pointer value can also be converted to an integral value. 根据以下规则,转换路径取决于指针的大小和整型的大小:The conversion path depends on the size of the pointer and the size of the integral type, according to the following rules:

  • 如果指针的大小大于或等于整型的大小,则指针的行为类似于转换中的无符号值,除非它无法转换为浮点值。If the size of the pointer is greater than or equal to the size of the integral type, the pointer behaves like an unsigned value in the conversion, except that it cannot be converted to a floating value.

  • 如果指针小于整型,则指针首先转换为与整型大小相同的指针,然后转换为整型。If the pointer is smaller than the integral type, the pointer is first converted to a pointer with the same size as the integral type, then converted to the integral type.

    相反,整型可以基于以下规则转换为指针类型:Conversely, an integral type can be converted to a pointer type according to the following rules:

  • 如果整型与指针类型的大小相同,则转换只会促使整数值被视为指针(无符号整数)。If the integral type is the same size as the pointer type, the conversion simply causes the integral value to be treated as a pointer (an unsigned integer).

  • 如果整型类型的大小与指针类型的大小不同,则使用表从带符号整型类型转换从无符号整型类型转换中给定的转换路径,首先将整型转换为指针的大小。If the size of the integral type is different from the size of the pointer type, the integral type is first converted to the size of the pointer, using the conversion paths given in the tables Conversion from Signed Integral Types and Conversion from Unsigned Integral Types. 然后将其视为一个指针值。It is then treated as a pointer value.

    带值 0 的整型常量表达式或到类型 void * 的表达式转换可以通过类型转换、赋值或者与指向任何类型的指针进行比较来进行转换。An integral constant expression with value 0 or such an expression cast to type void \* can be converted by a type cast, by assignment, or by comparison to a pointer of any type. 这将产生与同一类型的另一个 null 指针相等的 null 指针,但此 null 指针与指向函数或对象的任何指针不相等。This produces a null pointer that is equal to another null pointer of the same type, but this null pointer is not equal to any pointer to a function or to an object. 常数 0 以外的整数可以转换为指针类型,但结果是不可移植的。Integers other than the constant 0 can be converted to pointer type, but the result is not portable.

另请参阅See Also

赋值转换Assignment Conversions