Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      cast operator

      From cppreference.com
      <c‎ |language
       
       
       
       

      Performs explicit type conversion

      Contents

      [edit]Syntax

      (type-name)expression

      where

      type-name - either the typevoid or anyscalar type
      expression - anyexpression ofscalar type (unlesstype-name isvoid, in which case it can be anything)

      [edit]Explanation

      Iftype-name isvoid, thenexpression is evaluated for its side-effects and its returned value is discarded, same as whenexpression is used on its own, as anexpression statement.

      Otherwise, iftype-name is exactly the type ofexpression, nothing is done (except that ifexpression has floating type and is represented with greater range and precision than its type indicates – see below).

      Otherwise, the value ofexpression is converted to the type named bytype-name, as follows:

      Everyimplicit conversion as if by assignment is allowed.

      In addition to the implicit conversions, the following conversions are allowed:

      • Any integer can be cast to any pointer type. Except for the null pointer constants such asNULL (whichdoesn't need a cast), the result is implementation-defined, may not be correctly aligned, may not point to an object of the referenced type, and may be atrap representation.
      • Any pointer type can be cast to any integer type. The result is implementation-defined, even for null pointer values (they do not necessarily result in the value zero). If the result cannot be represented in the target type, the behavior is undefined (unsigned integers do not implement modulo arithmetic on a cast from pointer).
      • Any pointer to object can be cast to any other pointer to object. If the value is not correctly aligned for the target type, the behavior is undefined. Otherwise, if the value is converted back to the original type, it compares equal to the original value. If a pointer to object is cast to pointer to any character type, the result points at the lowest byte of the object and may be incremented up to sizeof the target type (in other words, can be used to examineobject representation or to make a copy viamemcpy ormemmove).
      • Any pointer to function can be cast to a pointer to any other function type. If the resulting pointer is converted back to the original type, it compares equal to the original value. If the converted pointer is used to make a function call, the behavior is undefined (unless the function types arecompatible).
      • When casting between pointers (either object or function), if the original value is a null pointer value of its type, the result is the correct null pointer value for the target type.

      In any case (both when executing an implicit conversion and in the same-type cast), ifexpression andtype-name are floating types andexpression is represented with greater range and precision than its type indicates (seeFLT_EVAL_METHOD), the range and precision are stripped off to match the target type.

      Thevalue category of the cast expression is always non-lvalue.

      [edit]Notes

      Becauseconst,volatile,restrict, and_Atomic qualifiers have effect onlvalues only, a cast to a cvr-qualified or atomic type is exactly equivalent to the cast to the corresponding unqualified type.

      The cast tovoid is sometimes useful to silence compiler warnings about unused results.

      The conversions not listed here are not allowed. In particular,

      • there are no conversions between pointers and floating types;
      • there are no conversions between pointers to functions and pointers to objects (includingvoid*).

      If the implementation providesintptr_t and/oruintptr_t, then a cast from a pointer to an object type (includingcvvoid) to these types is always well-defined. However, this is not guaranteed for a function pointer.

      (since C99)

      Note that conversions between function pointers and object pointers are accepted as extensions by many compilers, and expected by some usages of POSIXdlsym function.

      [edit]Example

      Run this code
      #include <stdio.h> int main(void){// examining object representation is a legitimate use of castconstdouble d=3.14;printf("The double %.2f (%a) is: ", d, d);for(size_t n=0; n!=sizeof d;++n)printf("%02X ",((unsignedchar*)&d)[n]); // edge casesstruct S{int x;} s;// (struct S)s; // error; not a scalar type, even though// casting to the same type does nothing(void)s;// okay to cast any type to void}

      Output:

      The double 3.14 (0x1.91eb851eb851fp+1) is: 1F 85 EB 51 B8 1E 09 40

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 6.5.5 Cast operators (p: 83-84)
      • C17 standard (ISO/IEC 9899:2018):
      • 6.5.4 Cast operators (p: 65-66)
      • C11 standard (ISO/IEC 9899:2011):
      • 6.5.4 Cast operators (p: 91)
      • C99 standard (ISO/IEC 9899:1999):
      • 6.5.4 Cast operators (p: 81)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 3.3.4 Cast operators

      [edit]See also

      C++ documentation forexplicit type conversion
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/language/cast&oldid=183160"

      [8]ページ先頭

      ©2009-2025 Movatter.jp