Announcement

Collapse

Maintenance

The forum could be offline for 30-60 minutes in the very near future for maintenance (said 3pm Pacific). I was behind on getting this notice. I do apologize.
See more
See less

Pointers to String Arrays

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Pointers to String Arrays

    I need (strong want) to access a group or unrelated string arrays
    by an index number.

    Apparently it is possible to get and save the ptr to an entire
    array, e.g.
    DIM R(1:4) as string
    DIM xptr AS STRING PTR
    xptr=VARPTR(R())

    but having saved it, what is the syntax to access the elements
    of the array,

    I've tried several ways. Here's two (commented out lines) that
    don't work.
    ----------------------------------------------------------------
    $COMPILE EXE
    FUNCTION PrintX(A() AS STRING) AS STRING
    FOR i&=1 TO 4:x$=x$+A(i&)+"|":NEXT:MSGBOX x$
    END FUNCTION

    FUNCTION PBMAIN() AS LONG
    DIM R(1:4) AS STRING
    R(1)=" String I. ":R(2)=" String II. "
    R(3)=" String III. ":R(4)=" String IV. "

    DIM xptr AS STRING PTR
    xptr=VARPTR(R())

    MSGBOX PrintX(R())
    'MSGBOX PrintX(xptr) ' This won't work
    'FOR i&=1 TO 4:[email protected](i&)+"|":NEXT:msgbox x$ ' Nor does this

    MSGBOX "end program"
    END FUNCTION

  • #2
    You're doing this the hard way.

    Just call your function, pass it the array...

    MyFunc$ = PrintX(R())

    FUNCTION PrintX (A() AS STRING ) AS STRING
    ... whatever

    END FUNCTION

    No need to mess with pointers here.
    MCM


    Michael Mattias
    Tal Systems Inc. (retired)
    Racine WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Michael, I'll grant you that if the idea was to code the example program
      I used, you are correct. But I'm looking at how to set up an array of
      pointers so that that I can access the array from a CALLBACK function
      using an index calculated from the CBCTL value. Inside the CALLBACK
      function all I have is a single number. I want to get to the array via
      its pointer.

      Comment


      • #4
        Well, why didn't say what you wanted?

        I asked about this a couple of weeks ago, and the answer was there is not in the current PB compilers any mechanism to access arrays given only the address of(pointer to)the array descriptor, but this is 'on the list' of features for consideration.

        What you want to do is not save a pointer to the array descriptor, but a pointer to the first element of the string array using VARPTR (not STRPTR) and use index pointers to get at the various elements. Treat the array elements as STRING PTRs and this should work (I did not test this).




        -------------
        Michael Mattias
        Racine WI USA
        [email protected]
        Michael Mattias
        Tal Systems Inc. (retired)
        Racine WI USA
        [email protected]
        http://www.talsystems.com

        Comment

        Working...
        X