Announcement

Collapse
No announcement yet.

Passing multidimensional array to a function?

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

  • Passing multidimensional array to a function?

    Hmm... i thought i read in another post that to pass a
    multidimensional array, you simply enter the number
    of dimensions in array in the Function declaration.

    e.g.
    Declare Function Pass2(test(2) as long) as long

    But this does not seem to be allowed. How does one pass
    a multidimensional array to a function?

    -Mike
    p.s. In a future version, id really like to see Types that
    can hold real array and not just "tables."


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

  • #2
    That is the technique employed in PB/DOS. In PB/CC and PB/DLL, you just pass the array descriptor -- that is, you leave the parentheses empty.

    Regarding real arrays within UDT's... you can easily do this by using DIM..AT to dim an aray over the top of the array member of the UDT. You still cannot use dynamic (variable-length) strings this way though, as they are not valid UDT members.



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

    Comment


    • #3
      Thanks. Passing the multidimensioanl array works. Can you show
      me an example of using Dim AT with an array in a UDT?

      If i have
      Type Test
      t(1 to 20) as long
      End Type

      and subsequently i fill the values of t()
      and then later want to dim a local variable
      as a LONG array, at the location of Test.t()
      can that be done without overwriting the contents
      of Test.t() ? I want to preserve the values already
      in test.t() and use the local array to reference
      it. Possible?

      Thanks
      -Mike

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

      Comment


      • #4
        I guess my confusion is, how do i Dim AT an "array" member
        of a UDT if it has no descriptor? It seems incorrect to
        DIM AT the first element of that UDT array member, but thats
        the only thing that seems to work.

        Code:
        #COMPILE EXE
        #DIM ALL
        DECLARE FUNCTION Pass(lp AS LONG ptr) AS LONG
        
        TYPE TEST
           x(0 TO 2) AS LONG
        END TYPE
        
        FUNCTION PBMAIN() AS LONG
           DIM tt AS TEST
           DIM lp AS LONG PTR
        
           tt.x(0) = 100
           tt.x(1) = 200
           tt.x(2) = 300
           lp = VARPTR(tt.x())  ' <-- Doesnt Work and if
                                '     I try using tt.x(0) then
                                '     that will result in the
                                '    local array in the following 
                                '    function to not be aligned properly
           
        END FUNCTION
        
        FUNCTION Pass (lp AS LONG PTR) AS LONG
           DIM t() AS LONG AT lp
           
           REDIM PRESERVE t(0 TO 2)
          
           MSGBOX "T.X(0) = " & STR$(t(0)) & " T.X(1) = " & STR$(t(1))
        END FUNCTION
        I hope this explanation makes sense. Is what im trying to
        do possible? Otherwise using Dim AT doesnt seem like its
        much use when working with "arrays" in UDTs.

        -Mike


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

        Comment


        • #5
          DIM..AT does not initialize the memory, it simply "maps" an array over the top of the existing memory.

          Code:
          #COMPILE EXE
           
          TYPE a
            b(1 TO 100) AS LONG
          END TYPE
           
          FUNCTION PBMAIN
              DIM z AS a
              z.b(75) = RND(1,100)
              DIM y(1:100) AS LONG AT VARPTR(z.b(1))
              ARRAY SORT y(), DESCEND
              MSGBOX STR$(y(1))
          END FUNCTION

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

          Comment


          • #6
            Excellent. Thanks. That works just the way i need it.
            -Mike

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

            Comment


            • #7
              uh,uh... since you are using DIM..AT, you cannot use REDIM PRESERVE... that would effectively move the memory (arrays almost always "move" during a REDIM). Since DIM..AT does not "own" the memory, you cannot move it this way... since the UDT member array is a fixed size (determined at compile time), you must limit your DIM..AT array to within the constraints of the member array size. If you want to resize your DIM..AT array, use REDIM..AT.

              For example:
              Code:
              #COMPILE EXE
              
              TYPE a
                b(1 TO 100) AS LONG
              END TYPE
              
              FUNCTION PBMAIN
                  DIM z AS a
                  z.b(75) = RND(1,100)
                  DIM y(1:100) AS LONG AT VARPTR(z.b(1))
                  ARRAY SORT y(), DESCEND
                  MSGBOX STR$(y(1)) + STR$(z.b(1))
                  REDIM y(1:50) AS LONG AT VARPTR(z.b(50))
                  z.b(75) = RND(1,100)
                  ARRAY SORT y(), DESCEND
                  MSGBOX STR$(y(1)) + STR$(z.b(50))
              END FUNCTION
              Essentially, DIM..AT is a powerful way of creating a psuedo-union with an array. However, if you want a truly dynamic and expandable array, use a normal array to start with.



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

              Comment

              Working...
              X