5.3.6.1 Combining Type Qualifiers And Pointers
It is helpful to first review the C conventions for definitions of pointer types.
Pointers can be qualified like any other 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 object 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 *
(i.e., 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 and that 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
. 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
could be externally modified.
In the second example, the pointer called ivp
is
volatile
, that is, the address the pointer contains could 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.
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 might not be universally
understood.