Theo's Forum

IT-Berater: Theo Gottwald (IT-Consultant) => General Tips and Discussion => Topic started by: Theo Gottwald on December 31, 2010, 09:47:24 AM

Title: Using Indexed String Pointers
Post by: Theo Gottwald on December 31, 2010, 09:47:24 AM
I found this one, deeply digged in the PB Forum (http://www.powerbasic.com/support/pbforums/showthread.php?t=5246&highlight=compiler+bug).

Watch the use of the stringptr @c[]!

Code: [Select]
#COMPILE EXE
#DIM ALL
#REGISTER NONE

FUNCTION PBMAIN

    DIM a(2) AS STRING
    DIM b(4) AS STRING
    DIM c AS STRING POINTER

    a(0) = "a0"
    a(1) = "a1"
    a(2) = "a2"

    b(0) = "b0"
    b(1) = "b1"
    b(2) = "b2"
    b(3) = "b3"
    b(4) = "b4"

    ' c = a
    c = VARPTR(a(0))
    MSGBOX @c[1]
    ' Returns "a1"

    ' c = b
    c = VARPTR(b(0))
    MSGBOX @c[3]
    ' Returns "b3"

    ' Change b
    b(3) = "TEST"
    MSGBOX @c[3]
    ' Returns "Test"

END FUNCTION
 

Another example:

Code: [Select]
#COMPILE EXE
#DIM ALL
#REGISTER NONE

FUNCTION PBMAIN

    DIM a(2) AS STRING
    DIM b(2) AS STRING
    DIM c(2) AS STRING
    LOCAL pc AS DWORD POINTER : pc = VARPTR(c())

    a(0) = "a0"
    a(1) = "a1"
    a(2) = "a2"

    b(0) = "b0"
    b(1) = "b1"
    b(2) = "b2"

    @pc = VARPTR(b(0))
    MSGBOX c(2)
    ' Returns "b2"

    b(2) = "TEST"
    MSGBOX c(2)
    ' Returns "TEST"

    @pc = VARPTR(a(0))
    MSGBOX c(2)
    ' Returns "a2"

END FUNCTION