I am not sure if this is the correct forum topic for posting this question, but ...
In the following sample code:
Can anyone tell me why the 2nd use of pointers fails? The 1st use of pointers is probably not yet implemented in version 1.7.7.0 so I will ignore that one, unless someone can point me in another direction (no pun intended).
[code=thinbasic]
' This test tries pointer dereferencing with @ptr
' taken from PowerBasic tutorial on pointers
' some stmts force abort w/version 1.7.7.0
' (probably un-implemented in this version)
uses "console"
dim ptr1 as byte ptr 'declares ptr1 as byte pointer
dim ptr2 as byte ptr 'declares ptr2 as byte pointer
dim byte1 as byte 'declares byte1 as byte variable
dim var1 as long 'declares var1 as long variable
dim byte2 as byte 'declares byte2 as byte variable
dim var2 as long 'declares var2 as long variable
dim dummy as byte 'for poke return addr
var1 = 54 'give value to var1
byte1 = 44 'give value to byte1
' <--- Method 1 causing abort
'the next pair of stmts forces abort
' (single quote comment allows script to run)
'ptr1 = varptr(byte1) 'point to byte1
'@ptr1 = 98 'change value of byte1
'printl "byte1 value changed via @ dereference = ", byte1
' <--- Method 2 causing abort
' the next pair of stmts forces abort
' either stmt by itself forces abort
' apparently varptr works only w/poke
' (single quote comment allows script to run)
'ptr1 = varptr(byte1) 'ptr1 points to byte1
'dummy = poke (byte, ptr1, 36) 'poke using predefined ptr
' <--- Now 3 sucessful methods
' how to change value of variable using pointer
printl "byte1 value before = ", byte1
'give new value to byte1 by looking at value of another variable
byte1 = peek(varptr(var1))
printl "byte1 value after = ", byte1
'this next statement allows changing variable value
'via pointer with computed expression
'note return value from poke is unstated and ignored
poke(byte, varptr(byte1), 36+4)
printl "byte1 (ignore poke return) = ", byte1
'get poke return value
dummy = poke (byte, varptr(byte2), 2
printl "dummy value return from poke = ", dummy
printl "byte2 (use poke return) = ", byte2
printl
printl "press any key to terminate"
waitkey
[/code]
Richard
Bookmarks