Shop OBEX P1 Docs P2 Docs Learn Events
array questions . . . — Parallax Forums

array questions . . .

Don PomplunDon Pomplun Posts: 116
edited 2007-01-23 10:45 in Propeller 1
can I:

1. have multidimensional arrays? e.g. VAR byte XX[noparse][[/noparse]20,2]

2. have arrays of constants? e.g. CON XX = 1,2,3

3. initialize VAR arrays simply? e.g. VAR byte XX = 1,2,3

syntaces I've tried don't seem to take. TIA,


suggested alternatives or refrences welcome.

-- Don

Comments

  • Don PomplunDon Pomplun Posts: 116
    edited 2007-01-23 03:57
    I saw a reference to defining a zero-element array. I winder if this will work:

    var long xx[noparse][[/noparse]0]
    con a=10, b=20, c=30

    I wonder if that will that make XX[noparse][[/noparse]0] be 10, XX be 20 and XX be 30 ??

    -- Don
  • Mike GreenMike Green Posts: 23,101
    edited 2007-01-23 03:59
    Don,
    1) No, although it's really easy to either do multiple dimension subscripting with one dimensional arrays or implement an array object where you have
    a function for producing a value (XXvalue(20,2)) or for storing it (XXsave(20,2,value)).
    2) You can have constant tables in a DAT section and the compiler takes the element size from the declaration of the first item in the table
    (XX long 1,2,3) and will let you use subscript notation to access the elements.
    3) Easiest is to use a constant table in a DAT section, just change the values from your Spin code with the usual subscript notation. The only place
    where there's a significant difference between these two approaches is when there are multiple instances of the same object. There's a unique VAR
    section set up for each instance, but only one shared DAT section.
    Mike
  • Mike GreenMike Green Posts: 23,101
    edited 2007-01-23 04:03
    Regarding your second posting: Named constants (CON section) don't exist in the compiled program. They're known only to the compiler. There's no memory allocated to them. I could be wrong, but I don't think it's legal to declare an array with zero size. I think the reference was to the notion of the zero'th element. Array indices start at zero so there's a zero'th element which is actually the first element, but with a subscript of zero.
  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-01-23 04:03
    My previous post (eaten by my expired cookie) suggested exact what Mike has.

    To expand on Mike's #1, to put a 2D array in a 1D array use [noparse][[/noparse]i+j*xdim] where i,j is your index and xdim is the number columns, the total size is [noparse][[/noparse]xdim*ydim].

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.

    Post Edited (Paul Baker (Parallax)) : 1/23/2007 10:45:39 AM GMT
  • Mike GreenMike Green Posts: 23,101
    edited 2007-01-23 04:08
    A practical example for a 2x3 array:
    CON aa = 2 ' x dimension
           bb = 3 ' y dimension
    VAR xx[noparse][[/noparse]aa * bb]
    PRI main
       repeat i from 0 to aa
          repeat j from 0 to bb
             xx[noparse][[/noparse]i * bb + j] = 0
    
    
  • Don PomplunDon Pomplun Posts: 116
    edited 2007-01-23 04:08
    I think I saw gthe zero-length reference in the Tricks & Traps section.
    e.g.
    VAR
    byte xx[noparse][[/noparse]0]
    byte A, B, C

    XX[noparse][[/noparse]0] will be the same as A, XX as B, etc.
    (unless I read it wrong -- still didn't help initialize the arrys, though)
    Thanx for the hints on DAT. I'll work that for a while.
    -- Don
  • Bill HenningBill Henning Posts: 6,445
    edited 2007-01-23 04:37
    Umm... I do believe you mean...

    myarray[noparse][[/noparse]y*xdim+x]

    [noparse]:)[/noparse]
    Paul Baker (Parallax) said...
    My previous post (eaten by my expired cookie) suggested exact what Mike has.

    To expand on Mike's #1, to put a 2D array in a 1D array use [noparse][[/noparse]i*j*xdim] where i,j is your index and xdim is the number columns, the total size is [noparse][[/noparse]xdim*ydim].

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.mikronauts.com - a new blog about microcontrollers
  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-01-23 10:45
    You are correct, thank you for pointing that out.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
Sign In or Register to comment.