#20 array references?

closed
nobody
None
5
2008-03-25
2007-08-12
Pavel Petrovic
No

I am not sure, but wouldn't it be possible with the standard nxt firmware to implement array references?

I.e. introduce a variable type, which could hold references to different global arrays.

If that would be possible, one could actually write functions that would operate on arrays, which seems to be impossible now without copying the whole array...?

I would find this feature very useful.

Discussion

  • s4ll4c
    s4ll4c
    2007-08-16

    Logged In: YES
    user_id=1868671
    Originator: NO

    if references on arrays really work, this should do it:

    // work with the original byteArray
    byte getSecond(byte &byteArray[])
    {
    return myArray[1];
    }

    task main()
    {
    byte byteArray[10];
    byte second;

    second = getSecond( byteArray );
    }

    short explanation:
    - instead of copying the whole byteArray, the compiler internally copies the pointer
    of the first array value when you call the method.
    - so you dont work with a copy of byteArray in getSecond(), but you access the byteArray itself.
    You don't loose CPU time for array copies.

     
  • s4ll4c
    s4ll4c
    2007-08-16

    Logged In: YES
    user_id=1868671
    Originator: NO

    sorry i forgot to change myArray in getSecond():

    byte getSecond(byte &byteArray[])
    {
    // return myArray[1];
    return byteArray[1];
    }

     
  • Pavel Petrovic
    Pavel Petrovic
    2007-08-16

    Logged In: YES
    user_id=9375
    Originator: YES

    That's what I mean. Would also be nice to see it in NBC - where probably a new type that could hold an array reference would have to be introduced (or simply use word for that...) Thus one could pass a reference to array to a function in global variable of such type. But I guess NXC itself is translated to NBC, so it would have to be there in one way or another. Still, I am very much missing an opperation for copying part of one array to part of another array. Now it is only possible to copy a whole array to part of another array, if I got the replace command right - but this, if I understand, is a limitation of the standard firmware? Any chances of pushing on LEGO to introduce new code in a firmware patch? :o)

     
  • John Hansen
    John Hansen
    2008-03-25

    • status: open --> closed
     
  • John Hansen
    John Hansen
    2008-03-25

    Logged In: YES
    user_id=661666
    Originator: NO

    There are no features within the standard NXT firmware that would allow me to implement array references. This would require an enhancement to the firmware.