HPlogo HP-UX/HP C Compiler: HP C/HP-UX Reference Manual > Chapter 3 Data Types and Declarations

Type Qualifiers

» 

Technical documentation

Complete book in PDF

 » Table of Contents

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;

Declares 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.

int const *volatile vpci;

Declares a volatile pointer to a constant int.

const *pci;

Declares a pointer to a constant int. Since no type specifier was given, it defaults to int.

 

When a type qualifier is used with a variable typed by a typedef name, the qualifier is applied without regard to the contents of the typedef. For example:

typedef int *t_ptr_to_int;
volatile t_ptr_to_int vol_ptr_to_int;

In the example above, the type of vol_ptr_to_int is volatile t_ptr_to_int, which becomes volatile pointer to int. If the type t_ptr_to_int were substituted directly in the declaration,

volatile int * ptr_to_vol_int;

the type would be pointer to volatile int.

Type qualifiers apply to objects, not to types. For example:

typedef int * t;
const t *volatile p;

In the example above, p is a volatile pointer to a const pointer to int. volatile applies to the object p, while const applies to the object pointed to by p. The declaration of p can also be written as follows:

t const *volatile p;

If an aggregate variable such as a structure is declared volatile, all members of the aggregate are also volatile.

If a pointer to a volatile object is converted to a pointer to a non-volatile type, and the object is referenced by the converted pointer, the behavior is undefined.

© Hewlett-Packard Development Company, L.P.