Results 1 to 2 of 2

Thread: Pointer methods

  1. #1

    Pointer methods

    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 so I will ignore that one, unless someone can point me in another direction (no pun intended).

    ' This test tries pointer dereferencing with @ptr
    ' taken from PowerBasic tutorial on pointers
    ' some stmts force abort w/version
    ' (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 "press any key to terminate"


  2. #2
    thinBasic MVPs
    Join Date
    May 2007
    Blog Entries
    Rep Power

    Re: Pointer methods

    Dim ptr1 As byte Ptr 'declares ptr1 as byte pointer
    Dim byte1 As Byte 'declares byte1 as byte variable

    ptr1 = VarPtr(byte1) 'point to byte1
    @ptr1 = 98 'change value of byte1
    PrintL "byte1 value changed via @ dereference = ", byte1

    DIM ptr1 as DWORD 'it has to hold a 32 bit address.
    ptr1 = VarPtr(byte1) 'point to byte1
    POKE (BYTE, ptr1, 9 'change value of byte1
    PrintL "byte1 value changed via Poke = ", byte1

    @ is used by Powerbasic not thinBasic.
    Home Desktop : Windows 7 - Intel Pentium (D) - 3.0 Ghz - 2GB - Geforce 6800GS
    Home Laptop : WinXP Pro SP3 - Intel Centrino Duo - 1.73 Ghz - 2 GB - Intel GMA 950
    Home Laptop : Windows 10 - Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz, 2401 Mhz, 2 Core(s), 4 Logical Processor(s) - 4 GB - Intel HD 4400
    Work Desktop : Windows 10 - Intel I7 - 4 Ghz - 8GB - Quadro Fx 370

Similar Threads

  1. Classes and Methods
    By Charles Pegge in forum O2h Compiler
    Replies: 8
    Last Post: 05-12-2011, 15:56

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts