NXC: Initialising 3 dimensional array

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
roscohead
Posts: 18
Joined: 29 Sep 2010, 03:09

NXC: Initialising 3 dimensional array

Post by roscohead »

The following code does not compile, getting "Error parsing expression: }" on the last line of the program. Can 3 dimension arrays be initialised like this, or do they have to be done in code?

ROSCO

Code: Select all

int MyArray[][][] = {
    {
      {0, -1},
      {1, -1},
    },
    {
      {2, -1},
      {3, -1},
    },
};

task main() {
     Wait(1000);
}
spillerrec
Posts: 358
Joined: 01 Oct 2010, 06:37
Location: Denmark
Contact:

Re: NXC: Initialising 3 dimensional array

Post by spillerrec »

I think the compiler simply does not support initializing 3D arrays at the moment, so you would probably have to do it in code right now. It doesn't work with initializing a 2D array of a LocationType struct either, so I guess it is the three levels of brackets which is confusing it.
So do a workaround or wait until John tells whether he is going to fix it or not.
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC: Initialising 3 dimensional array

Post by afanofosc »

I have made a compiler change to NXC which has enabled global and local array initialization for 1-4 dimension arrays.

Code: Select all

int MyArray[][][][] = {
  {
    {{0, -1},{1, -1}},
    {{2, -1},{3, -1}}
  },
  {
    {{10, 11},{12, 13}},
    {{20, 21},{23, 31}}
  }
};

task main() {
  NumOut( 0, LCD_LINE1, MyArray[0][0][0][0]);
  NumOut(20, LCD_LINE1, MyArray[0][0][0][1]);
  NumOut(40, LCD_LINE1, MyArray[0][0][1][0]);
  NumOut(60, LCD_LINE1, MyArray[0][0][1][1]);

  NumOut( 0, LCD_LINE2, MyArray[0][1][0][0]);
  NumOut(20, LCD_LINE2, MyArray[0][1][0][1]);
  NumOut(40, LCD_LINE2, MyArray[0][1][1][0]);
  NumOut(60, LCD_LINE2, MyArray[0][1][1][1]);

  NumOut( 0, LCD_LINE3, MyArray[1][0][0][0]);
  NumOut(20, LCD_LINE3, MyArray[1][0][0][1]);
  NumOut(40, LCD_LINE3, MyArray[1][0][1][0]);
  NumOut(60, LCD_LINE3, MyArray[1][0][1][1]);

  NumOut( 0, LCD_LINE4, MyArray[1][1][0][0]);
  NumOut(20, LCD_LINE4, MyArray[1][1][0][1]);
  NumOut(40, LCD_LINE4, MyArray[1][1][1][0]);
  NumOut(60, LCD_LINE4, MyArray[1][1][1][1]);

  while(true);
}
As it is currently written, it works with or without the trailing commas that Rosco's code above uses. It zaps the extra trailing commas by replacing all occurrences of ',}' with '}' and of course the parser has already removed all whitespace. Then it chunks up the initialization string recursively until it gets to 2d or below where it falls back to the original initialization code by searching for a string of DIM-1 '}' followed by a comma, i.e., '}},' for 3d arrays and '}}},' for 4d arrays.

Multi-dimensional arrays of structs still cannot be initialized in this manner. I will see if I can roll into the revised code a check for a base type of UDT (aka user defined type, aka struct) and create the required number of temporary instances of the right struct type statically initialized and then arrbuild them into the recursively higher dimensioned arrays. That might not happen right away.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
roscohead
Posts: 18
Joined: 29 Sep 2010, 03:09

Re: NXC: Initialising 3 dimensional array

Post by roscohead »

Wow! Thanks for the unexpected update John! I really didn't mind initialising in code, I just wanted to make sure I wasn't doing something wrong! But now that you've gone to all that trouble, I guess I'd better use it ;)

ROSCO
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: Initialising 3 dimensional array

Post by HaWe »

How will this work with ArrayInit()?
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC: Initialising 3 dimensional array

Post by afanofosc »

This doesn't have anything to do with using ArrayInit to manually initialize arrays.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
sharpsee
Posts: 3
Joined: 24 Nov 2011, 07:22

Re: NXC: Initialising 3 dimensional array

Post by sharpsee »

I'm having the exact same problem as rosco, tested with the code posted here by John Hansen. I'm running bricxcc 3.3.8.9, which is the latest update I can find on bricxcc.sourceforge.net. Is this compiler change a coming update, or can someone point me in the right direction for finding it?

Thanks
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: NXC: Initialising 3 dimensional array

Post by muntoo »

Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
sharpsee
Posts: 3
Joined: 24 Nov 2011, 07:22

Re: NXC: Initialising 3 dimensional array

Post by sharpsee »

Hm.. still having the same problem with
test_release20110720.zip
test_release20111024.zip

Is it something I'm doing wrong?
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: NXC: Initialising 3 dimensional array

Post by muntoo »

Try opening a new thread if the problem persists, and link to this thread.
  • Have you flashed the latest Enhanced Firmware (RFW file in the test release) to your NXT?
  • Have you configured the compiler options correctly?
    • Go to Edit -> Preferences->Compiler->NBC/NXC
    • Enable NXT 2.0 Compatible Firmware
    • Enable Enhanced Firmware
    • Enable Use internal compiler
    • Enable Automatic firmware version
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
Post Reply

Who is online

Users browsing this forum: No registered users and 16 guests