Casting Pointers
In the C language, casting is a construct to view a data object temporarily as another data type.
When you cast pointers, especially for non-data object pointers, consider the following characteristics and constraints:
- You can cast a pointer to another pointer of the same IBM® i pointer type. Note: If the ILE C compiler detects a type mismatch in an expression, a compile time error occurs.
- An open (void) pointer can hold a pointer of any type. Casting an open pointer to other pointer types and casting other pointer types to an open pointer does not result in a compile time error. Note: You might receive a runtime exception if the pointer contains a value unsuitable for the context.
- When you convert a valid data object pointer to a signed or unsigned integer type, the return value is the offset of the pointer. If the pointer is NULL, the conversion returns a value of zero (0). Note: It is not possible to determine whether the conversion originated from a NULL pointer or a valid pointer with an offset 0.
- When you convert a valid function (procedure) pointer, system pointer, invocation pointer, label pointer, or suspend pointer to a signed or unsigned integer type, the result is always zero.
- When you convert an open pointer that contains a valid space address, the return value is the offset that is contained in the address.
- You can convert an integer to pointer, but the resulting pointer value cannot be dereferenced. The right four bytes of such a pointer will contain the original integer value, and this value can be recovered by converting the pointer back to an integer. Note: This marks a change from behavior exhibited in earlier versions of ILE C, where integer to pointer conversions always resulted in a NULL pointer value.
Example:
Figure 1 shows IBM i pointer casting: