HP C/iX Reference Manual (31506-90011)

Chapter 3 33
Data Types and Declarations
Type Qualifiers
Type Qualifiers
Syntax
type-qualifier
::= const
volatile
Description
This section describes the type qualifiersvolatile and const.
The volatile type qualifier directs the compiler not to perform certain optimizations on
an object because that object can have its value altered in ways beyond the control of the
compiler.
Specifically, when an object's declaration includes the volatile type qualifier,
optimizations that would delay any references to (or modifications of) the object will not
occur across sequence points. A
sequence point
is a point in the execution process when
the evaluation of an expression is complete, and all side-effects of previous evaluations
have occurred.
The volatile type qualifier is useful for controlling access to memory-mapped device
registers, as well as for providing reliable access to memory locations used by
asynchronous processes.
The const type qualifier informs the compiler that the object will not be modified, thereby
increasing the optimization opportunities available to the compiler.
An assignment cannot be made to a constant pointer, but an assignment can be made to
the object to which it points. An assignment can be made to a pointer to constant data, but
not to the object to which it points. In the case of a constant pointer to constant data, an
assignment cannot be made to either the pointer, or the object to which it points.
Type qualifiers may be used alone (as the sole declaration-specifier), or in conjunction with
type specifiers, including struct, union, enum, and typedef. Type qualifiers may also
be used in conjunction with storage-class specifiers.
Table 3-2 illustrates various declarations using the const and volatile type qualifiers.
Table 3-2. Declarations using const and volatile
Declaration Meaning
volatile int vol_int; Delclares a volatile int variable.
const int *ptr_to_const_int;
int const *ptr_to_const_int;
Both declare a variable pointer to a constant int.
int *const const_ptr_to_int; Declares a constant pointer to a variable int.
int *volatile vpi, *pi; Declares two pointers: vpi is a volatile pointer to
an int; pi is a pointer to an int.