Microchip Technology SW006022-2N Fiche De Données

Page de 338
Supported Data Types and Variables
 2012 Microchip Technology Inc.
DS52071B-page 99
6.6.2
Data Pointers
All standard data pointers are 16 bits wide. This is sufficient to access the full data 
memory space.
These pointers are also able to access const-qualified objects, although in the pro-
gram memory space, const-qualified objects appear in a unique memory range in the 
data space using the PSV window. In this case, the -mconst-in-data option should 
not be in force (see Section 3.7.1 “Options Specific to 16-Bit Devices”.)
Pointers which access the managed PSV space are 32-bits wide. The extra space 
allows these pointers to access any PSV page.
A set of special purpose, 32-bit data pointers are also available. See Chapter 
7. “Memory Allocation and Access”
 fo
r more information.
6.6.3
Function Pointers
The MPLAB XC16 C Compiler fully supports pointers to functions, which allows func-
tions to be called indirectly. Function pointers are always 16 bits wide.
In the small code model (up to 32 kWords of code), 16-bit wide function pointers can 
access any function location. In the large code model, which supports more than 32 
kWords of code, pointers hold the address of a GOTO instruction in a lookup table. 
These instructions are able to reach any memory location, but the lookup table itself is 
located in the lower program memory, thus allowing the pointers themselves to remain 
as 16-bit wide variables.
As function pointers are only 16-bits wide, these pointers cannot point beyond the first 
64K of FLASH. Should the address of a function that is allocated beyond the first 64K 
of FLASH be taken, the linker will arrange for a handle section to be generated. The 
handle
 section will always be allocated within the first 64K. Each handle provides a 
level of indirection which allows 16-bit pointers to access the full range of FLASH. This 
operation may be disable with the --no-handles linker option.
6.6.4
Special Pointer Targets
Pointers and integers are not interchangeable. Assigning an integer value to a pointer 
will generate a warning to this effect. For example:
const char * cp = 0x123;  // the compiler will flag this as bad code
There is no information in the integer, 0x123, relating to the type, size or memory loca-
tion of the destination. Avoid assigning an integer (whether it be a constant or variable) 
to a pointer at all times. Addresses assigned to pointers should be derived from the 
address operator “&“ that C provides.
In instances where you need to have a pointer reference a seemingly arbitrary address 
or address range, consider defining an object or label at the desired location. If the 
object is defined in assembly code, use a C declaration (using the extern keyword) 
to create a C object which links in with the external object and whose address can be 
taken.