Download mikroPascal PRO - MikroElektronika

Transcript
mikroPascal PRO for dsPIC30/33 and PIC24
Note: Comparing pointers pointing to different objects/arrays can be performed at programmer’s own responsibility — a
precise overview of data’s physical storage is required.
Pointer Addition
You can use Inc to add an integral value to a pointer. The result of addition is defined only if the pointer points to an
element of an array and if the result is a pointer pointing to the same array (or one element beyond it).
If a pointer is declared to point to type, adding an integral value n to the pointer increments the pointer value by n *
sizeof(type) as long as the pointer remains within the legal range (first element to one beyond the last element). If
type has a size of 10 bytes, then adding 5 to a pointer to type advances the pointer 50 bytes in memory.
For example:
var
a : array[10] of byte;
// array a containing 10 elements of type byte
ptr : ^byte;
// pointer to byte
begin
ptr := @a[0];
// ptr is pointer to byte, pointing to a[0]
ptr := ptr + 3;
// ptr+3 is a pointer pointing to a[3]
ptr^ := 6;
// a[3] now equals 6
Inc(ptr);
// ptr now points to the next element of array a: a[4]
end.
Also, you may sum values pointed to by pointers.
For example:
var
i, j, x : byte; // variables
ptr1 : ^byte;
// pointers to byte
ptr2 : ^byte;
begin
i := 10;
j := 5;
ptr1 := @i;
ptr2 := @j;
// assign value 10 to variable; i is at the address 0x0038
// assign value 10 to variable; j is at the address 0x003A
// ptr1 is pointer to byte, pointing to i
// ptr2 is a pointer pointing to j
x := ptr1^ + ptr2^;
end.
// result is equal to the sum of the values pointed to; x = 5
Pointer Subtraction
Similar to addition, you can use Dec to subtract an integral value from a pointer.
If a pointer is declared to point to type, subtracting an integral value n from the the pointer decrements the pointer
value by n * sizeof(type) as long as the pointer remains within the legal range (first element to one beyond the
last element). If type has a size of 10 bytes, then subtracting 5 from a pointer to type pushes back the pointer 50 bytes
in memory.
210
MikroElektronika