[Coco] VARPTR not what expected.

William Astle lost at l-w.ca
Sun Jul 3 19:24:48 EDT 2022


On 2022-07-03 13:54, coco--- via Coco wrote:
> All
> 
> I Wrote the following program to try to understand how the VARPTR in 
> Color Computer 3
> Disk Extended Color Basic 2.1 works.
> 
> 5  WIDTH 80
> 7  CLS 5
> 10 DIM XP$(10)
> 15 AD=0
> 20 XP$(1)="ABCDE"
> 30 XP$(2)="CAB"
> 40 AD=VARPTR(XP$(1))
> 50 PRINT "AT ADDRESS =";AD
> 55 FOR A=0 TO 10
> 56 :PA=AD+A
> 57 :PRINT PEEK(PA);
> 58 :PRINT " **";
> 59 NEXT A

Because PA and A are defined *after* the VARPTR call, the value AD gets 
is out of date by the time you use it.

In memory, the scalar (non array) variables immediately follow the 
program. Then the arrays immediately follow the scalars. If you cause a 
new scalar to be allocated, the arrays are relocated.

Try defining PA and A before the VARPTR call.

Then keep in mind that AD will point to a descriptor for the string 
rather than the string itself. (Length is 1 byte at offset 0, address of 
data is 2 bytes at offset 2). The string data is not NUL terminated.

-- 
William Astle


More information about the Coco mailing list