Cast operator ()

Read syntax diagramSkip visual syntax diagram
Cast expression syntax

>>-(--type--)--expression--------------------------------------><

C onlyIn C, the result of the cast operation is not an lvalue.C only

C++ onlyIn C++, the cast result belongs to one of the following value categories:
  • If type is an lvalue reference type C++11or an rvalue reference to a function typeC++11, the cast result is an lvalue.
  • C++11If type is an rvalue reference to an object type, the cast result is an xvalue.C++11
  • In all other cases, the cast result is a C++11(prvalue)C++11 rvalue.
C++ only
The following example demonstrates the use of the cast operator to dynamically create an integer array of size 10:
#include <stdlib.h>

int main(void) {
   int* myArray = (int*) malloc(10 * sizeof(int));
   free(myArray);
   return 0;
}
The malloc library function returns a void pointer that points to memory that holds an object of the size of its argument. The statement int* myArray = (int*) malloc(10 * sizeof(int)) has the following steps:
  • Creates a void pointer that points to memory that can hold ten integers.
  • Converts that void pointer into an integer pointer with the use of the cast operator.
  • Assigns that integer pointer to myArray.
Begin C++ only In C++ you can also use the following objects in cast expressions:
  • Function-style casts
  • C++ conversion operators, such as static_cast.
Function-style notation converts the value of expression to the type type:
type(expression)
The following example shows the same value cast with a C-style cast, the C++ function-style cast, and a C++ cast operator:
#include <iostream>
using namespace std;

int main() {
  float num = 98.76;
  int x1 = (int) num;
  int x2 = int(num);
  int x3 = static_cast<int>(num);

  cout << "x1 = " << x1 << endl;
  cout << "x2 = " << x2 << endl;
  cout << "x3 = " << x3 << endl;
}
See the output of the above example:
x1 = 98
x2 = 98
x3 = 98
The integer x1 is assigned a value in which num has been explicitly converted to an int with the C-style cast. The integer x2 is assigned a value that has been converted with the function-style cast. The integer x3 is assigned a value that has been converted with the static_cast operator.

For C++, the operand of a cast expression can have class type. If the operand has class type, it can be cast to any type for which the class has a user-defined conversion function. Casts can invoke a constructor, if the target type is a class, or they can invoke a conversion function, if the source type is a class. They can be ambiguous if both conditions hold. End C++ only

Cast to union type (C only) (IBM extension)

Casting to a union type is the ability to cast a union member to the same type as the union to which it belongs. Such a cast does not produce an lvalue. The feature is supported as an extension to C99, implemented to facilitate porting programs developed with GNU C.

Only a type that explicitly exists as a member of a union type can be cast to that union type. The cast can use either the tag of the union type or a union type name declared in a typedef expression. The type specified must be a complete union type. An anonymous union type can be used in a cast to a union type, provided that it has a tag or type name. A bit field can be cast to a union type, provided that the union contains a bit field member of the same type, but not necessarily of the same length. The following code shows an example of a simple cast to union:
#include <stdio.h>

union f {
   char t;
   short u;
   int v;
   long w;
   long long x;
   float y;
   double z;
};

int main() {
  union f u;
  char a = 1;
  u = (union f)a;
  printf("u = %i\n", u.t);
} 
The output of this example is:
u = 1
Casting to a nested union is also allowed. In the following example, the double type dd can be cast to the nested union u2_t.
int main() {
   union u_t {
      char a;
      short b;
      int c;
      union u2_t {
         double d;
      }u2;
   };
   union u_t U;
   double dd = 1.234;
   U.u2 = (union u2_t) dd;      // Valid.
   printf("U.u2 is %f\n", U.u2);
}
The output of this example is:
U.u2 is 1.234
A union cast is also valid as a function argument, part of a constant expression for initialization of a static or non-static data object, and in a compound literal statement. The following example shows a cast to union used as part of an expression for initializing a static object:
struct S{
  int a;
}s;

union U{
  struct S *s;
};

struct T{
  union U u;
};

static struct T t[] = { {(union U)&s} };     


Voice your opinion on getting help information Ask IBM compiler experts a technical question in the IBM XL compilers forum Reach out to us