Announcement

Collapse
No announcement yet.

VarPtr and arrays..???

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

  • VarPtr and arrays..???

    Ok, here is a weird one....

    The following code WILL work correctly,,, But why..?

    Type StationRecord
    Number As String * 5
    sData(1600 To 2100, 12) As Double
    End Type

    Function PbMain()

    Dim Station As StationRecord
    Dim RainArr() as double

    '--- Add some data to Station ----

    ReDim RainArr(1600 to 2200, 12) as double At VarPtr(Station.sData(1500,0))

    End Function


    OK, Why do I have to ReDim at 1500 rather than 1600 to get the data in the right spot...?????

    TIA
    Torben

  • #2
    Torben --
    What do you name "correctly" ?
    RainArr(x, 0) and Station.sData(x,0) will have different location (like expected)

    Code:
      #Compile Exe
      #Dim All
      #Register None
      Type StationRecord
         Number As String * 5
         sData(1600 To 2100, 12) As Double
      End Type
      Function PbMain() As Long
         Dim Station As StationRecord
         Dim RainArr() As Double
         '--- Add some data to Station ----
         ReDim RainArr(1600 To 2200, 12) As Double At VarPtr(Station.sData(1500,0))
         MsgBox Format$(VarPtr(RainArr(1700, 0))),, Format$(VarPtr(Station.sData(1700,0)))
      End Function


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

    Comment


    • #3
      Semen,

      I mean that the data I put in Station.sData(1800,1)
      will only appear in RainArr(1800,1) if I ReDim the RainArr at 1500 rather than 1600...

      I presume it has something to do with the fact that I am also trying to extend the ReDim'ed array at the same time. ie, I am increasing the UBOUND from 2100 to 2200. But what I don't understand is why does that mean I have to bring the LBOUND back the same amount...????

      Torben


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

      Comment


      • #4
        Torben --
        imagine following
        Redim Ar1(1 : 100, 1 : 6) At x
        Redim Ar2(1 : 200, 1 : 3) At x

        Elements are located
        Ar1(1, 1) ... Ar1(100, 1) Ar1(1, 2) ... Ar1(100, 2)
        Ar2(1, 1) ... Ar2(200, 1) Ar2(1, 2) ... Ar1(200, 2)

        This means that address of Ar1(1, 3) is the same as Ar2(1, 2)
        Probably, if you will calculate addresses for your case, you will understand, what happends.

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

        Comment


        • #5
          Multi-dimensional arrays are stored in "column-major" order... see ARRAY SORT() for more information on how arrays are stored internally.



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

          Comment

          Working...
          X