Shop OBEX P1 Docs P2 Docs Learn Events
How to initialize arrays — Parallax Forums

How to initialize arrays

peterzpeterz Posts: 59
edited 2007-01-10 20:07 in Propeller 1
I have used a naive way to initialize arrays because I don't know how to delcare an array and initialize it at the same time.

I do:

BYTE MyArray[noparse][[/noparse]5]

MyArray[noparse][[/noparse]0]:=$23
MyArray[noparse][[/noparse]1]:=$20
MyArray[noparse][[/noparse]2]:=$11
MyArray[noparse][[/noparse]3]:=$21
MyArray[noparse][[/noparse]4]:=$AA

I wanted to do something like:

BYTE MyArray[noparse][[/noparse]5]=$23,$20,$11,$21,$AA

How can I do it in Spin ?

Comments

  • Jasper_MJasper_M Posts: 222
    edited 2007-01-10 19:18
    You could define the array in DAT section:

    MyArray BYTE $23,$20,$11,$21,$AA

    And then you would use it from Spin code like any other array: MyArray[noparse][[/noparse]index]

    But be aware that no aligning takes place. ie. if you have an array of longs (or just plain longs), they may be at address $13 and whatnot, ie. addresses that aren't divisible by 4 and therefore not accessible by rdlong (and maybe some spin commands, I don't really know about spin, but assignment and comparing and stuff seems to work.)

    EDIT: You should of course leave out the BYTE MyArray[noparse][[/noparse]whatever]. If you want an array longer than the value it should be initialized to, add zeros after the BYTE (or WORD or LONG) in DAT to make a larger array.

    Post Edited (Jasper_M) : 1/10/2007 7:22:58 PM GMT
  • peterzpeterz Posts: 59
    edited 2007-01-10 19:50
    These numbers in an array declared as such are writable ?
    I am trying to modify them without success.

    I pass the array to another OBJ with @MyArray.
    It works as expected, but then I try to modify the values with:

    MyArray:=$55

    And call again the other object. But it keeps the old value!
    Is this normal ?
  • Jasper_MJasper_M Posts: 222
    edited 2007-01-10 19:54
    How about MyArray[noparse][[/noparse]0] := 55? And yes, they are writeable. And... I think you should put there PointerToMyArray.BYTE[noparse][[/noparse]0] := 55 ... It recognizes them as bytes only if you use them directly, not as parameter. (Again, I'm not 100% sure.)
  • Jasper_MJasper_M Posts: 222
    edited 2007-01-10 19:59
    Umm forget everything I said in the last post. It won't work... This will work:

    When passing it as parameter:

    PUB Function(Pointer)
    BYTE[noparse][[/noparse]Pointer][noparse][[/noparse]Index] := something

    And it'd be called with @MyArray as argument,.
  • peterzpeterz Posts: 59
    edited 2007-01-10 19:59
    My fault. I was writing out of the array !

    So, if I pass the array as a pointer as follows it wont work ?

    MyFunction(@MyArray)

    BYTE b
    MyFunction(array)

    b := array[noparse][[/noparse]0]

    Would I need the following:

    BYTE b
    MyFunction(array)

    b := BYTE[noparse][[/noparse]array][noparse][[/noparse]0]

    ???




    Post Edited (peterz) : 1/10/2007 8:04:57 PM GMT
  • Jasper_MJasper_M Posts: 222
    edited 2007-01-10 20:07
    But if you put the pointer as parameter, it still won't work like Parameter[noparse][[/noparse]index]. It won't work like that with normal arrays either.
Sign In or Register to comment.