Type qualifiers
A type qualifier is used to refine the declaration of
a variable, a function, and parameters, by specifying whether:
- The value of an object can be changed
- The value of an object must always be read from memory rather than from a register
- More than one pointer can access a modifiable memory address
z/OS XL
C/C++ includes the following additional type qualifiers to meet the
special needs of the z/OS environment:
The C++ standard 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, for a pointer to a volatile
data object:
volatile int *x; /* x is a pointer to a volatile int */
or int volatile *x; /* x is a pointer to a volatile int */
For
a pointer to a
const
data object: 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, and the compiler ignores duplicate type qualifiers. This is a C99 language feature. To be compatible with C99, the z/OS XL C/C++ compiler supports it as an IBM extension.
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 it
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 variable, they also apply to the members of the structure, class or union.