User manual
mikroBasic PRO for PIC32
MikroElektronika
205
Pointers
A pointer is a data type which holds a memory address. While a variable accesses that memory address directly, a
pointer can be thought of as a reference to that memory address.
To declare a pointer data type, add a carat prex (^) before type. For example, in order to create a pointer to an
integer, write:
^integer
In order to access data at the pointer’s memory location, add a carat after the variable name. For example, let’s declare
variable p which points to a word, and then assign value 5 to the pointed memory location:
dim p as ^word
‘...
p^ = 5
A pointer can be assigned to another pointer. However, note that only the address, not the value, is copied. Once you
modify the data located at one pointer, the other pointer, when dereferenced, also yields modied data.
Pointers and memory spaces
Pointers can point to data in any available memory space.
Pointers can reside in any available memory space except in program (code) memory space.
dim ptr1 as ^const byte ‘ ptr1 pointer in data space pointing to a byte in code space
dim ptr2 as ^const ^volatile sfr byte rx ‘ ptr2 is pointer in rx space pointing to a
pointer in code space pointing to volatile byte in sfr space
dim ptr3 as ^data byte code ‘ error, pointers can not be placed in code space
Due to backward compatibility, pointers to program memory space can also be declared within constant declaration
block (using keyword const):
program const_ptr
‘ constant array will be stored in program memory
const b_array as byte[5] = (1,2,3,4,5)
const ptr as ^byte ‘ ptr is pointer to program memory space
main:
ptr = @b_array ‘ ptr now points to b_array[0]
PORTA = ptr^
ptr = ptr + 3 ‘ ptr now points to b_array[3]
PORTA = ptr
end.