Microchip Technology SW006023-2N Data Sheet
MPLAB
®
XC32 C/C++ Compiler User’s Guide
DS51686E-page 100
2012 Microchip Technology Inc.
6.7
POINTER TYPES
There are two basic pointer types supported by the MPLAB XC32 C/C++ Compiler:
data pointers and function pointers. Data pointers hold the addresses of variables
which can be indirectly read, and possible indirectly written, by the program. Function
pointers hold the address of an executable function which can be called indirectly via
the pointer.
data pointers and function pointers. Data pointers hold the addresses of variables
which can be indirectly read, and possible indirectly written, by the program. Function
pointers hold the address of an executable function which can be called indirectly via
the pointer.
6.7.1
Combining Type Qualifiers and Pointers
It is helpful to first review the ANSI C/C++ standard conventions for definitions of
pointer types.
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:
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 * (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 tar-
gets. 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.
variable itself. The type and any qualifiers to the left of the * relate to the pointer’s tar-
gets. 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:
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 derefer-
enced are treated as being volatile. In other words, the target objects accessible via
the pointer may be externally modified.
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 derefer-
enced 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.
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.
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.
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.
6.7.2
Data Pointers
Pointers in the compiler are all 32 bits in size. These can hold an address which can
reach all memory locations.
reach all memory locations.
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.
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.