Page 1 of 2

NXC: Initialising 3 dimensional array

Posted: 01 Nov 2011, 03:00
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);
}

Re: NXC: Initialising 3 dimensional array

Posted: 01 Nov 2011, 07:14
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.

Re: NXC: Initialising 3 dimensional array

Posted: 01 Nov 2011, 18:07
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

Re: NXC: Initialising 3 dimensional array

Posted: 04 Nov 2011, 19:02
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

Re: NXC: Initialising 3 dimensional array

Posted: 05 Nov 2011, 08:09
by HaWe
How will this work with ArrayInit()?

Re: NXC: Initialising 3 dimensional array

Posted: 08 Nov 2011, 17:14
by afanofosc
This doesn't have anything to do with using ArrayInit to manually initialize arrays.

John Hansen

Re: NXC: Initialising 3 dimensional array

Posted: 24 Nov 2011, 07:33
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

Re: NXC: Initialising 3 dimensional array

Posted: 24 Nov 2011, 09:44
by muntoo

Re: NXC: Initialising 3 dimensional array

Posted: 24 Nov 2011, 16:52
by sharpsee
Hm.. still having the same problem with
test_release20110720.zip
test_release20111024.zip

Is it something I'm doing wrong?

Re: NXC: Initialising 3 dimensional array

Posted: 25 Nov 2011, 03:53
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