Type qualifiers

A type qualifier is used to refine the declaration of a variable, a function, and parameters, by specifying whether:

XL C/C++ recognizes the following type qualifiers:

Standard C++ refers to the type qualifiers const and volatile as cv-qualifiers. In both languages, the cv-qualifiers are only meaningful in expressions that are lvalues.

When the const and volatile keywords are used with pointers, the placement of the qualifier is critical in determining whether it is the pointer itself that is to be qualified, or the object to which the pointer points. For a pointer that you want to qualify as volatile or const, 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 and qualifier can be in any order, provided that the qualifier does not follow the * operator. 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     */

The following examples contrast the semantics of these declarations:

Declaration Description
const int * ptr1; Defines a pointer to a constant integer: the value pointed to cannot be changed.
int * const ptr2; Defines a constant pointer to an integer: the integer can be changed, but ptr2 cannot point to anything else.
const int * const ptr3; Defines a constant pointer to a constant integer: neither the value pointed to nor the pointer itself can be changed.

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

A type qualifier cannot apply to user-defined types, but only to objects created from a user-defined type. Therefore, the following declaration is illegal:

      volatile struct omega {
                               int limit;
                               char code;

However, if a variable or variables are declared within the same definition of the type, a type qualifier can be applied to the variable or variables by placing at the beginning of the statement or before the variable declarator or declarators. Therefore:

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

provides the same storage as:

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

In both examples, the volatile qualifier only applies to the structure variable group.

When type qualifiers are applied to a structure, class, or union, or class variable, they also apply to the members of the structure, class or union.

Related information