9.6.1 Combining Type Qualifiers and Pointers

It is helpful to first review the ANSI C/C++ standard conventions for definitions of pointer types.

Pointers can be qualified like any other C/C++ object, but care must be taken when doing so as there are two quantities associated with pointers. The first is the actual pointer itself, which is treated like any ordinary C/C++ variable and has memory reserved for it. The second is the target, or targets, that the pointer references, or to which the pointer points. The general form of a pointer definition looks like the following:

target_type_&_qualifiers * pointer’s_qualifiers pointer’s_name;

Any qualifiers to the right of the * (that is, next to the pointer’s name) relate to the pointer variable itself. The type and any qualifiers to the left of the * relate to the pointer’s targets. This makes sense since it is also the * operator that dereferences a pointer, which allows you to get from the pointer variable to its current target.

Here are three examples of pointer definitions using the volatile qualifier. The fields in the definitions have been highlighted with spacing:

volatile int *           vip ;
int          * volatile  ivp ;
volatile int * volatile  vivp ;

The first example is a pointer called vip. It contains the address of int objects that are qualified volatile. The pointer itself — the variable that holds the address — is not volatile; however, the objects that are accessed when the pointer is dereferenced are treated as being volatile. In other words, the target objects accessible via the pointer may be externally modified.

The second example is a pointer called ivp which also contains the address of int objects. In this example, the pointer itself is volatile, that is, the address the pointer contains may be externally modified; however, the objects that can be accessed when dereferencing the pointer are not volatile.

The last example is of a pointer called vivp which is itself qualified volatile, and which also holds the address of volatile objects.

Bear in mind that one pointer can be assigned the addresses of many objects; for example, a pointer that is a parameter to a function is assigned a new object address every time the function is called. The definition of the pointer must be valid for every target address assigned.

Note: Care must be taken when describing pointers. Is a “const pointer” a pointer that points to const objects, or a pointer that is const itself? You can talk about “pointers to const” and “const pointers” to help clarify the definition, but such terms may not be universally understood.