Announcement

Collapse
No announcement yet.

ARRAY POINTER

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

  • Lance Edmonds
    replied
    YOu are correct in that there are two ways to pass an array to a sub or function... the following information is found in the Inline Assembler chapter of the Help File:
    Passing arrays

    Each array in your program has an associated array descriptor. This descriptor is saved in a proprietary format, which may change from version to version of PowerBASIC. Since most of the information in the descriptor is not relevant to assembler code, it is usually best to simply pass a pointer to the first element of the array instead. You can use the VARPTR() function to retrieve that address. Subsequent elements of the array will immediately follow the first in memory, while multi-dimensional arrays are stored in column-major order.
    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>

    Leave a comment:


  • Charles Dietz
    replied
    It seems to me that there are two different ways to pass an array
    to a sub or function. Both ways actually pass an address but they
    differ in how the address is taken.

    Method 1: pass array by reference
    Method 2: pass pointer to first element of array

    Code:
       #COMPILE EXE
       #DIM ALL
       SUB ShowArray1(x() AS STRING, n AS INTEGER)
          MSGBOX x(n),, "Method 1"
       END SUB
       SUB ShowArray2(x AS STRING PTR, n AS INTEGER)
          MSGBOX @x[n - 1],, "Method 2"
       END SUB
    
       FUNCTION PBMAIN() AS LONG
          DIM S(1:10) AS STRING
          DIM SPtr AS STRING PTR
          LOCAL i AS INTEGER
          FOR i=1 TO 10
             S(i)=STR$(100+i) + " " + STRING$(i, "z")
          NEXT
          SPtr=VARPTR(S(1))
          ShowArray1 BYREF s(), 4      'method 1
          ShowArray2 BYVAL SPtr, 7     'method 2 
       END FUNCTION
    ------------------

    Leave a comment:


  • Lance Edmonds
    replied
    The UUB token pair [ CODE ] and [ /CODE ] tell the BBS software to preserve leading whitespace - this has the effect of preserving the code indenting so code is easier to read (with out the tokens, the BBS displays the text with leading whitespace stripped off).

    Which of the following is easier to read?

    for x& = 1 to 10
    a = b + c + d + e
    for y! = 1.1! to 2.1!
    e = y! + z! + q!
    if y! > 2! then exit for
    next
    a = b + c + d + e
    next
    Code:
    for x& = 1 to 10
      a = b + c + d + e
      for y! = 1.1! to 2.1!
        e = y! + z! + q!
        if y! > 2! then exit for
      next 
      a = b + c + d + e
    next

    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>

    Leave a comment:


  • Joe Speroni
    replied
    Lance, I guess I was not clear (as usual) about why I was trying
    to understand the syntax for handling string pointer parameters.
    I understood how to pass an array as a parameter to a function,
    in fact up at the top of this thread my first code submission
    showed exactly that.

    The problem I was trying to solve is how could a CALLBACK
    function which has no parameters (other than CBCTL) access
    the string array associated with the CONTROL ADD COMBOBOX
    that created it. It can be done with a series of IF CBCTL=xxx,
    ELSEIF CBCTL=yyy, ... , etc. But this is unwieldly and I was
    looking for a way to just have an array of pointers indexed
    by CBCTL to get to the array. I have around 50 COMBOXBOXES
    and the code was getting out of hand!

    I now "understand" the syntax to get this to work and I'd
    recommend an example including such a usage.

    The following code is a better example of what I was trying to
    do (ShowS3 no longer has a parameter, like a CALLBACK function).

    BTW I don't know what UBB stands for, but I searched the forum
    and came up with your recommentation to put {code}, and
    {/code} bracketing the code (where { and } are square brackets). I don't know
    It is not clear why this is needed (get rid of spaces from
    copy/paste operation?). I can access the code w/o them? But
    I'll be sure to include them in the future. Does this address
    the UBB code problem? Please clarify if I'm still not doing
    it right.

    And as always thanks to you and the many others who make this
    forum so valuable! This forum should be a bigger part of
    your advertising -- in addition to a great compiler.

    Aloha,

    Joe Speroni
    -----------------------------------------------------------------
    Code:
    #COMPILE EXE
    GLOBAL SPtr AS STRING PTR
    
    FUNCTION GetElement(X() AS STRING,i AS LONG) AS STRING
    FUNCTION=X(i):END FUNCTION
    
    FUNCTION ShowS3() AS LONG
    MSGBOX GetElement(BYVAL SPtr,3)
    END FUNCTION
    
    FUNCTION PBMAIN() AS LONG
    DIM S(1:10) AS STRING:FOR i&=1 TO 10:S(i&)=STR$(100+i&):NEXT
    DIM T(1:20) AS STRING:FOR i&=1 TO 20:T(i&)=STR$(200+i&):NEXT
    DIM SPtr AS STRING PTR
    SPtr=VARPTR(S())
    ShowS3
    SPtr=VARPTR(T())
    ShowS3
    END FUNCTION
    ------------------

    Leave a comment:


  • Lance Edmonds
    replied
    You dont need to DIM a String Pointer just to get the addressm you can pass the address of the array directly to the function:

    Code:
    ShowS3 BYVAL VARPTR(s())
    Of course, this is exactly the same as just passing the array, afterall!

    Code:
    ShowS3 s()
    Joe, can you please use UUB codes when posting your source code...? Thanks!


    ------------------
    Lance
    PowerBASIC Support
    mailto:[email protected][email protected]</A>

    Leave a comment:


  • Joe Speroni
    replied
    Well Lasse, your code was not quite what I wanted, but I used
    it to produce the following. I want to make an array of
    string pointers (indexed by a COMBOBOX ID, and pointing to
    the string array that have all the text for that COMBOBOX's
    list. I greatly simplies the CallBack functions.

    The code I got working follows. Thanks much for the samples
    that got me working on the problem.
    --------------------------------------------------------------
    #COMPILE EXE
    FUNCTION ShowS3(x() AS STRING) AS LONG
    MSGBOX x(4)+"|"+str$(UBOUND(x))
    END FUNCTION

    FUNCTION PBMAIN() AS LONG
    DIM S(1:10) AS STRING
    FOR i&=1 TO 10:S(i&)=STR$(100+i&):NEXT
    DIM SPtr AS STRING PTR:SPtr=VARPTR(S())
    ShowS3 BYVAL SPtr
    END FUNCTION


    ------------------

    Leave a comment:


  • LRantanen
    Guest replied
    A typo !!!

    Instead of

    FUNCTION ShowS3(x AS STRING PTR) AS LONGINCR x

    of course there should be

    FUNCTION ShowS3(x AS STRING PTR) AS LONG

    Sorry,

    Lasse Rantanen
    [email protected]

    Leave a comment:


  • LRantanen
    Guest replied
    Joe,

    if you want to pass to sub the pointer to an string array and an index to this array, you have to do e.g. following

    Code:
    FUNCTION ShowS3(x AS STRING PTR, idx AS INTEGER) AS LONG
       LOCAL i AS INTEGER
       
       FOR i = 0 TO idx - 2
           INCR x
       NEXT
    
       MSGBOX @X
    
    END FUNCTION
    
    FUNCTION PBMain
        DIM S(1:10) AS STRING
        DIM SPtr AS STRING PTR
        
        FOR i&=1 TO 10
            S(i&)=STR$(100+i&)
        NEXT
        
        SPtr=VARPTR(S(1))
        ShowS3 BYVAL SPtr, 3
    END FUNCTION
    or if you dont have to pass the index as argument, do following

    Code:
    FUNCTION ShowS3(x AS STRING PTR) AS LONGINCR x
    
       MSGBOX @X
    
    END FUNCTION
    
    FUNCTION PBMain
        DIM S(1:10) AS STRING
        DIM SPtr AS STRING PTR
        
        FOR i&=1 TO 10
            S(i&)=STR$(100+i&)
        NEXT
        
        SPtr=VARPTR(S(3))
        ShowS3 BYVAL SPtr
    END FUNCTION
    Hope this helps,

    Lasse Rantanen
    [email protected]

    Leave a comment:


  • Joe Speroni
    started a topic ARRAY POINTER

    ARRAY POINTER

    The following program does not compile correctly because
    I'm trying to pass the pointer of an array to a function
    instead of the array itself. Is there a syntax that will
    accomplish this?
    ------------------------------------------------------------
    #COMPILE EXE
    #REGISTER NONE
    #INCLUDE "Win32Api.Inc"

    FUNCTION ShowS3(x() AS STRING) AS LONG
    MSGBOX X(3)
    END FUNCTION

    FUNCTION PBMAIN()
    DIM S(1:10) AS STRING:FOR i&=1 TO 10:S(i&)=STR$(100+i&):NEXT
    SPtr&=VARPTR(S())
    'ShowS3 S() ' works fine, but how to pass a pointer instead?
    ShowS3 SPtr&
    END FUNCTION

Working...
X