HP C A.06.05 Reference Manual

Expressions and Operators
Cast Operator
Chapter 598
Cast Operator
Syntax
(
data_type
)
exp
Arguments
data_type
Any scalar data type, including a scalar data type created through a
typedef
. The
data_type
cannot be an aggregate type, but it can be a
pointer to an aggregate type.
exp
Any scalar expression.
Description
To cast a value means to explicitly convert it to another data type. For example, given the two
definitions:
int y = 5;
float x;
The following cast operation casts the value of y to float:
x = (float) y; /* x now equals 5.0 */
Here are four more casts (assume that j is a scalar data type):
i = (float) j; /* Cast j's value to float */
i = (char *)j; /* Cast j's value to a pointer to a char */
i = ((int *)())j; /* Cast j's value to a pointer */
/* to a function returning an int */
i = (float) (double) j; /* Cast j's value first to a double */
/* and then to a float */
It is important to note that if exp is a variable, a cast does not change this variable's data type;
it only changes the type of the variable's value for that one expression. For instance, in the
preceding casting examples, the cast does not produce any permanent effect on variable j.
There are no restrictions on casting from one scalar data type to another, except that you may
not cast a void object to any other type. You should be careful when casting integers to
pointers. If the integer value does not represent a valid address, the results are unpredictable.
A cast expression may not be an lvalue.