NXC array declaration

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
ricardocrl
Posts: 117
Joined: 27 Dec 2010, 19:27

Re: NXC array declaration

Post by ricardocrl »

The problem also exists without dimensions specified. I will then post my example. Maybe it is another problem, but also affecting the initialization.

This is the code example:

Code: Select all

float myArray[][] = {  {0.0, 1.0, 0.0}, {0.0, 0.5, 1.0} };

task main(void){
  ClearScreen();
  NumOut(0, LCD_LINE1, myArray[0][0]);
  NumOut(20, LCD_LINE1, myArray[0][1]);
  NumOut(40, LCD_LINE1, myArray[0][2]);

  NumOut(0, LCD_LINE2, myArray[1][0]);
  NumOut(20, LCD_LINE2, myArray[1][1]);
  NumOut(40, LCD_LINE2, myArray[1][2]);
  
  while(true);
}
myArray[1][2] ends up "0", instead of "1.0". In fact, from some more tests, I got:

What doesn't work(for myArray[1]):
{0.0, 0.5, 1.0}
{0.0, 0.5, 1}
{0.0, 0.5, 2.0}
{0.0, 0.5, 3}
What works - correctly initialized and displayed:
{0.0, 0.5, 1.1}
{0.0, 0.5, 1.5}
{1, 2, 3}
{0.0, 0.5, 3}

I did most of this tests using Optimization level 0, 1 and 2 and also tried with FW 1.2.9, 1.3.1 and the latest 1.3.2 (from August 2012).

I could be missing something...
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC array declaration

Post by afanofosc »

Hmm, it does look like floating point array initialization is broken if the values are a mixture of whole numbers (such as 1.0) and real numbers (such as 1.5). It looks like this has been broken for a very long time.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
drdaron
Posts: 2
Joined: 14 Oct 2012, 19:08

Re: NXC array declaration

Post by drdaron »

This could well be a newbie problem (I am just starting on NXC) and can't seem to get the following to put anything in the Answer array (its just showing me a load of '0' values...I've been hitting my head against this for hours, rejigging the code in various ways and always following the examples. I am using the latest firmware 1.31. Any help would be much appreciated.

Code: Select all

int Answer[5][5];

task main()
{
  for (int j=0; j<5; j++)
 {
  for (int i=0; i<5; i++)
  {
        Answer[i][j] = 1+Random(8);
        NumOut(i*8,j*8,Answer[i][j]);
  }
 }
 Wait(5000);
}
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC array declaration

Post by HaWe »

your firmware 1.31 is not the latest fw and probably not the enhanced fw ;)
try this (maybe by Google translate):
http://www.mindstormsforum.de/viewtopic.php?f=25&t=3311

the good news:
with the latest BCC beta version and efw it works fine!
drdaron
Posts: 2
Joined: 14 Oct 2012, 19:08

Re: NXC array declaration

Post by drdaron »

Thanks for the reply - i will give it a try!
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC array declaration

Post by afanofosc »

Just to clarify, while NXC supports multi-dimensional arrays, the standard LEGO firmware does not work correctly with arrays of more than 1 dimension due to a bug in the REPLACE opcode. That bug is fixed in the enhanced NBC/NXC firmware. With the standard firmware you will not be able to use anything but 1-dimensional arrays. An array of strings is actually a 2-dimensional array since each string is an array of bytes.

If you choose to use NXC then you really should switch from the standard LEGO firmware to the enhanced NBC/NXC firmware, the latest version of which you can find here:

http://bricxcc.sourceforge.net/test_releases/

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests