Announcement

Collapse
No announcement yet.

Problems with array insert

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

    Problems with array insert

    When I pass an array to ARRAY INSERT with a type pointer the compiler (PBDLL6) doesn't recognise that is actually an array. The following short program demonstrates
    Code:
    TYPE testa
         a(999) AS BYTE
    END TYPE
    GLOBAL ta() AS testa
    GLOBAL tp AS testa PTR
    SUB testinsert ()
       REDIM ta(999)
       tp = VARPTR(ta(2))
       ARRAY INSERT @tp.a(100) FOR 100 ,0
    END SUB
    The compiler gives error 423 array variable expected

    If I remove the pointer and pass the array directly as in the next example
    Code:
    TYPE testa
         a(999) AS BYTE
    END TYPE
    GLOBAL ta() AS testa
    SUB testinsert ()
       REDIM ta(999)
       ARRAY INSERT ta(2).a(100) FOR 100 ,0
    END SUB
    the compiler gives error 477 syntax error
    If I make it even simpler
    Code:
    TYPE testa
         a(999) AS BYTE
    END TYPE
    GLOBAL ta AS testa
    SUB testinsert ()
       ARRAY INSERT ta.a(100) FOR 100 ,0
    END SUB
    the compiler again gives error 423

    I have large arrays of complex UDT's which contain large arrays of unions and was hoping I could insert and delete from them quickly without resorting to assemler instructions.
    Any suggestions?

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

    #2
    the array statement requires a "normal" array, not an array within a udt (a udt member array)

    in general, the solution is to use dim..at to "overlay" a conventional array over the udt member array.

    see http://www.powerbasic.com/support/pb...ad.php?t=15034 for a similar discussion/example.



    ------------------
    lance
    powerbasic support
    mailto:[email protected][email protected]</a>
    Lance
    mailto:[email protected]

    Comment


      #3
      Thanks Lance I had forgotten that one.
      Can I impose on your skills a little further?
      I realise the pointers in PB are very powerful, do you think the following code would get the correct overlay address? Sorry if it seems lazy of me but the project is many thousands of lines (and thus hard to test) and that area will probably have to be optimised into assembler in the final version.
      Code:
      UNION testu
            b(999) AS BYTE
            l(249) AS LONG
      END UNION
      TYPE testa
           s AS STRING * 20
           u AS testu
      END TYPE
      GLOBAL ta() AS testa
      globaltp AS testa PTR
      SUB testarray ()
           REDIM ta(9999)
           tp = VARPTR(ta(167)
           DIM TempByteArray(999) AS BYTE AT VARPTR(@tp.u.b(0))
      END SUB
      ------------------

      Comment


        #4
        You may be doing this the hard way.

        Take a look at using pointer offsets, as in:
        Code:
           X = @p[offsetvalue]
        I haven't looked at your problem in depth, but I think this might make your code easier to read and maintain.

        Using this method, you never need a BYTE array; you just use either a BYTE PTR or STRING PTR * 1.


        MCM


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

        Comment

        Working...
        X
        😀
        🥰
        🤢
        😎
        😡
        👍
        👎