Type Qualifiers

C recognizes three type qualifiers, const, volatile, and restrict. C++ refers to the type qualifiers const and volatile as cv-qualifiers and recognizes the type qualifier restrict as a language extension. In both languages, the cv-qualifiers are only meaningful in expressions that are lvalues. C++ allows a cv-qualifier to apply to functions, which is disallowed in C. The type qualifier restrict may only be applied to pointers.

Syntax for the const and volatile keywords

For a volatile or const pointer, you must put the keyword between the * and the identifier. For example:

      int * volatile x;        /* x is a volatile pointer to an int */
      int * const y = &z;      /* y is a const pointer to the int variable z */

For a pointer to a volatile or const data object, the type specifier, qualifier, and storage class specifier can be in any order. For example:

      volatile int *x;         /* x is a pointer to a volatile int
         or                                                         */
      int volatile *x;         /* x is a pointer to a volatile int  */
 
      const int *y;            /* y is a pointer to a const int
         or                                                         */
      int const *y;            /* y is a pointer to a const int     */

In the following example, the pointer to y is a constant. You can change the value that y points to, but you cannot change the value of y:

      int * const y

In the following example, the value that y points to is a constant integer and cannot be changed. However, you can change the value of y:

      const int * y

For other types of volatile and const variables, the position of the keyword within the definition (or declaration) is less important. For example:

      volatile struct omega {
                               int limit;
                               char code;
                            } group;

provides the same storage as:

      struct omega {
                      int limit;
                      char code;
                   } volatile group;

In both examples, only the structure variable group receives the volatile qualifier. Similarly, if you specified the const keyword instead of volatile, only the structure variable group receives the const qualifier. The const and volatile qualifiers when applied to a structure, union, or class also apply to the members of the structure, union, or class.

Although enumeration, class, structure, and union variables can receive the volatile or const qualifier, enumeration, class, structure, and union tags do not carry the volatile or const qualifier. For example, the blue structure does not carry the volatile qualifier:

      volatile struct whale {
                           int weight;
                           char name[8];
                      } beluga;
      struct whale blue;

The keywords volatile and const cannot separate the keywords enum, class, struct, and union from their tags.

You can declare or define a volatile or const function only if it is a nonstatic member function. You can define or declare any function to return a pointer to a volatile or const function.

An item can be both const and volatile. In this case the item cannot be legitimately modified by its own program but can be modified by some asynchronous process.

You can put more than one qualifier on a declaration: the compiler ignores duplicate type qualifiers. IBM Copyright 2003