OWBasic for Pocketviewer

Home INTRO Group POINTERS Alphabetical Index

Pointer operations

OWBasic allows to declare and to use pointer variables. To stay compatible with older programs, pointers are implicitly dereferenced; for getting or setting their value the special pointer syntax has to be used. It is also possible to declare pointer arrays; there is another special syntax for their use.

To declare a pointer variable, simply assign a value to it:
&<pointer><type-suffix> = <int-expression>

If you do not want to assign the pointer a specific value, it is recommended to assign the symbolic constant NULL:
&<pointer><type-suffix> = #NULL

Normally you assign the address of another variable to a pointer. To get a variable's address, use the &-operator:
&<pointer><type-suffix> = &<variable>

A pointer can also point to an array element:
&<pointer><type-suffix> = &<variable>[<int-expression>]

To get the address another pointer is pointing at, use the &-operator:
&<pointer2><type-suffix> = &<pointer>

You may indice a pointer for easier using of arrays:
<variable> = <pointer>[<int-expression>]
This code sets <variable> to the value of the variable at <pointer> + <int-expression>.

You can declare a pointer array by using DIM:
DIM &<pointer-array><type-suffix>[<constant expression>]

To have access to a pointer array's elements, you must use this syntax:
&~<pointer-array>[<int-expression>] = <int-expression>

A pointer array is readed the same way:
&<pointer> = &~<pointer-array>[<int-expression>]

To get or to set the value an element of a pointer array is pointing at you must use the following syntax:
~<pointer>[<int-expression>] = <expression>
<variable> = ~<pointer>[<int-expression>]

You can also indice a pointer array's member:
~<pointer>[<int-expression>][<int-expression2>] = <expression>
<variable> = ~<pointer>[<int-expression>][<int-expression2>]

Sometimes you want to use the address of a pointer itself; f.e. that may be useful for iterating over an array:
INC &&<pointer>
INC &&<pointer>[<int-expression>]

CARE: That does not work this way with pointers to Float. Float variables require more memory than Integer variables (Char- and Bool- variables are handled as integers internally, string variables contain a pointer to the string itself). To iterate over a Float array, you have to increment the pointer by FLOATSIZE:
INC &&<float-pointer>,#FLOATSIZE
INC &&<float-pointer>[<int-expression>],#FLOATSIZE
FLOATSIZE is a global constant and contains the size of float in integer steps.

To assign a pointer an integer value, the integer value can be converted by POINTER@(i%) where @ is the type suffix.

A pointer that is not implicitly dereferenced (f.e. if returned by a function) can be dereferenced explicitly using the operator '*':

i=*(&array+2) !: same effect as i=array[2]

Pointer and pointer arrays cannot be indiced two-dimensionally.

Pointers may be dangerous when not used properly; f.e. a index on a pointer variable is not range-checked.


Home INTRO Group POINTERS Alphabetical Index