HPlogo HP C/HP-UX Reference Manual: Workstations and Servers > Chapter 5 Expressions

Cast Operators

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

The cast operator is used to convert an expression of one type to another type.

Syntax

cast-expression ::=
unary-expression
(type-name) cast-expression

Description

An expression preceded by a parenthesized type name causes the expression to be converted to the named type. This operation is called a cast. The cast does not alter the type of the expression, only the type of the value. Unless the type name specifies void type, the type name must specify a scalar type, and the operand must have scalar type.

The result of a cast operation is not an lvalue.

Conversions involving pointers (other than assignment to or from a "pointer to void" or assignment of a null pointer constant to a pointer) require casts.

A pointer can be cast to an integral type and back again provided the integral type is at least as wide as an int.

A pointer to any object can safely be converted to a pointer to char or a pointer to void, and back again. If converted to a pointer to char, it will point to the first (lowest address) byte of the original object. For example, a pointer to an integer converted to a character pointer points to the most significant byte of the integer.

A pointer to a function of one type can safely be converted to a pointer to a function of another type, and back again.