Pointer Arithmetic
Pointer arithmetic in the mikroBasic PRO for PIC is limited to:
- assigning one pointer to another,
- comparing two pointers,
- comparing pointer to zero,
- adding/subtracting pointer and an integer value,
- subtracting two pointers.
Assignment and Comparison
The simple assignment operator (=
) can be used to assign value of one pointer to another if they are of the same type.
Assigning the integer constant 0 to a pointer assigns a null pointer value to it.
Two pointers pointing to the same array may be compared by using relational operators =
, <>
, <
, <=
, >
, and >=
. Results of these operations are the same as if they were used on subscript values of array elements in question:
dim ptr1 as ^byte ptr2 as ^byte a as byte[10] ' array a containing 10 elements of type byte main: ptr1 = @a[4] ptr2 = @a[2] if (ptr1 = ptr2) then ... ' won't be executed as 4 is not equal to 2 if (ptr1 > ptr2) then ... ' will be executed as 4 is greater than 2 if (ptr1^ = ptr2^) then ... ' if the value pointed to by ptr1 is equal to the value pointed to by ptr2 ... if (ptr1^ > ptr2^) then ... ' if the value pointed to by ptr1 is greater to the value pointed to by ptr2 ... end.

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:
dim a as byte[10] ' array a containing 10 elements of type byte ptr as ^byte ' pointer to byte main: 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:
dim i, j, x as byte ' variables ptr1 as ^byte ' pointers to byte ptr2 as ^byte main i = 10 ' assign value 10 to variable; i is at the address 0x0038 j = 5 ' assign value 10 to variable; j is at the address 0x003A ptr1 = @i ' ptr1 is pointer to byte, pointing to i ptr2 = @j ' ptr2 is a pointer pointing to j x = ptr1^ + ptr2^ ' result is equal to the sum of the values pointed to; x = 5 end.
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.
For example:
dim a as byte[10] ' array a containing 10 elements of type byte ptr as ^byte ' pointer to byte main: ptr = @a[6] ' ptr is pointer to byte, pointing to a[6] ptr = ptr - 3 ' ptr-3 is a pointer pointing to a[3] ptr^ = 6 ' a[3] now equals 6 Dec(ptr) ' ptr now points to the previous element of array a: a[2] end.
Also, you may subtract two pointers. The difference will be equal to the distance between two pointed addresses, and is calculated regarding to the type which the pointer points to.
For example:
dim i, j, x as byte ' variables ptr1 as ^byte ' pointers to byte ptr2 as ^byte main: i = 10 ' assign value 10 to variable; i is at the address 0x0039 j = 5 ' assign value 5 to variable; j is at the address 0x003A ptr1 = @i ' ptr1 is pointer to byte, pointing to i ptr2 = @j ' ptr2 is a pointer pointing to j x = ptr2 - ptr1 ' result is equal to the distance between the two pointed addresses; x = 1 (1 byte) x = ptr1^ - ptr2^ ' result is equal to the difference of the values pointed to; x = 5 end.
What do you think about this topic ? Send us feedback!