Commodore‎ > ‎BASIC‎ > ‎Keywords‎ > ‎

POINTER

Keyword Abbreviation Token (hex) Version(s) Classification
POINTER PO{Shift+I} CE 0A 7.0 Function

  Syntax  
POINTER ( variable )
 
Parameters Type Legal Value(s) Default Value Note(s)
variable Variable Name
any legal name
user-functions are not allowed
 
Returns Type Value(s) Note(s)
address Unsigned Integer
0 to 65535
 
  Purpose  
Finds the address in RAM where a variable is stored.
 
  Remarks  
The POINTER function finds the given variable in RAM and returns the address of its data.  The returned address does not point to the leading bytes which name the variable (or for arrays, that give the dimensions). 
 
It useful to pass the address of a variable to an ML program so it does not need to search for it, and also your BASIC code won't need to convert the value inside the variable into high and low bytes.  POINTER is also useful for loading/saving variable data to a file or REU.
 
POINTER does not return the bank where the variable is stored.  On the C128, this is always in BANK 1.  On the Plus/4, it doesn't matter; there is only one bank as far as BASIC is concerned (need to verify this!).
 
If you are religous about defining all variables before any are used, you can even assign the value of POINTER to another variable.  Then you can pass that variable to your ML program or use it to load/save the data.  This is dangerous with arrays because whenever you create a scalar (non-array) variable, all they arrays get moved around in memory.
 
POINTER is less useful with strings.  The address returned will point to the string descriptor.  The descriptor tells the length of the string and is followed by another two bytes (also called a pointer) which tell where the actual characters of the string can be found.  Also, string data gets shuffled around when BASIC performs garbage collection or the string is assigned a new value.
 
POINTER does not work at all with user-functions (see DEF).
 
For arrays, you must specify an element in the array.  If you want to load/save an entire array, use the first ("zeroth") element.  See examples.
 
If the variable does not exist, or is a reserved variable, POINTER returns zero.  If you omit variable, supply a literal value, or user-function name, then SYNTAX ERROR is generated.
 
Examples:
CLR : A = 1 : DIM B(9,9) :REM define scalar 'A' and 10x10 array 'B'

READY.
PRINT POINTER(A)
 1026
 
READY.
PRINT POINTER(B)
 0                   scalar B does not exist

READY.
PRINT POINTER(B(0,0)) : REM first element of array
 1040

READY.
C = 1 : REM create new scalar 'C', this moves all arrays!

READY.
PRINT POINTER(B(0,0))
 1047                it looks like C is 7 bytes in size

READY.
BANK 1: STASH 500,POINTER(B(0,0)), 0,0 : REM save array to REU address 0

READY.
C = POINTER(B(0,0)) : REM save pointer to make BSAVE easier

READY.
BSAVE "ARRAY B", B1, P(C) TO P(C+500)

SAVING 0:ARRAY B
READY.
 
  Compare With  
 
  Contrast With  
 
  See Also  

© H2Obsession, 2014
Comments