Microchip Technology SW006021-2 ユーザーズマニュアル
MPLAB
®
XC8 C Compiler User’s Guide
DS52053B-page 152
2012 Microchip Technology Inc.
5.4.5
Pointer Types
There are two basic pointer types supported by the MPLAB XC8 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.
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.
To conserve memory requirements and reduce execution time, pointers are made dif-
ferent sizes and formats. The MPLAB XC8 C Compiler uses sophisticated algorithms
to track the assignment of addresses to all pointers, and, as a result, non-standard
qualifiers are not required when defining pointer variables. The standard qualifiers
const
ferent sizes and formats. The MPLAB XC8 C Compiler uses sophisticated algorithms
to track the assignment of addresses to all pointers, and, as a result, non-standard
qualifiers are not required when defining pointer variables. The standard qualifiers
const
and volatile can still be used and have their usual meaning. Despite this, the
size of each pointer is optimal for its intended usage in the program.
5.4.5.1
COMBINING TYPE QUALIFIERS AND POINTERS
It is helpful to first review the ANSI C standard conventions for definitions of pointer
types.
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 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:
so as there are two quantities associated with pointers. The first is the actual pointer
itself, which is treated like any ordinary 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.
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.