Page 1 of 1

array-issues

Posted: 22 Aug 2013, 13:53
by merijn0301
]Hi,
I am writing some code to work arrays. It shouldn't be too hard, still I run into some problems.

If I try to assign a value like this..

arrTest[1] = "Greetings"

... the value is not assigned.

I tried to use "ArrayReplace()" but I get an errormessage because this function does nog exist althoug the website (brixcc) does show an explain it.

So : How do I assign a value to an array-element?

Merijn

Re: array-issues

Posted: 22 Aug 2013, 14:47
by mattallen37
What type is the variable "arrTest" (how did you declare it)?

Typically I only use arrays to hold numbers, not text. I use each element to contain one number.

Each text character is equivalent to an 8-bit number (typically represented by it's ASCII number), so a string is actually an array of 8-bit numbers (NULL terminated).

NXC does support multi-dim arrays, including of type "string", so it should be possible to do what you want. It all depends on how you declared the array (or re-initialized it).

Re: array-issues

Posted: 22 Aug 2013, 20:43
by afanofosc
If you don't have ArrayReplace then you probably have an old compiler. Are you running on Windows with BricxCC or on Linux or Mac OS X? If you are running on Windows have you updated to the latest test release from http://bricxcc.sourceforge.net/test_releases/?

More importantly, you probably are not using the enhanced NBC/NXC firmware which is required if you want to be able to use arrays with more than one dimension. A string array is actually a two dimensional byte array. The standard LEGO firmware only supports arrays with one dimension - at least with respect to the REPLACE opcode which is what gets used when you try to change the value of an item in an array.

In any case, if you are using the enhanced NBC/NXC firmware then you will be able to use multi-dimensional arrays, including arrays of strings.

It is also possible that your code is having trouble due to some quirks with how NXC handles array declarations/initialization but that usually leads to runtime aborts rather than simply a value in an array not being replaced like it should have been.

John Hansen

Re: array-issues

Posted: 26 Aug 2013, 07:11
by merijn0301
Hi,

I installed the Nxc/NBC firmware and it work perfrectly.
Indeed I was running the standard 'LEGO"-firmware.

Thank's for the replies