NXC array declaration

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

NXC array declaration

Post by mattallen37 »

Why is it that in NXC you can't declare and initialize an array like this?

byte array[10] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};

It will declare it, but it will be empty (all elements will be 0).

In order to make it initialize, it needs to be like this:

byte array[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};

where you don't specify the length (other than through the initialization part).
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC array declaration

Post by afanofosc »

There is not a particularly good reason why NXC works like this other than that I haven't been strongly encouraged to change that behavior. Should the size be ignored when an initialization is provided or should it limit/grow the array? I.e., if you say 5 but provide 6 values what should the size be? Or if you say 10 but provide 3 values? In C compilers I have used you get a compiler error if the size is less than the number of values in the initialization. It zero-fills to the specified size if the number of values in the initialization is less than the specified size.

I could spend some time changing this but I personally am of the opinion that if you provide an initial value for an array it is better practice to not specify a size since that can be the source for hard-to-find defects.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXC array declaration

Post by mattallen37 »

I understand the dilemma. Could you make it throw a compiler error if the size and the number of elements initialized don't match? I just spent one to two hours looking for a bug, and it turned out to be because of this. I would rather it throw a compiler error than to not initialize with the values that I hard-coded in.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC array declaration

Post by afanofosc »

I have made some changes to the compiler to handle this issue for 1-dimensional arrays. I will see if I can extend this without a huge effort to 2+ dimensions. I will also spend some time on structure initialization, which is woefully lacking at the moment.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXC array declaration

Post by mattallen37 »

Thanks for the effort!
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC array declaration

Post by afanofosc »

You can grab a new test release from http://bricxcc.sourceforge.net/test_releases/

I have NOT extended the array initialization changes to 2+ dimensional arrays or to structures at this point.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXC array declaration

Post by mattallen37 »

Thanks, downloading test release now :)
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
ricardocrl
Posts: 117
Joined: 27 Dec 2010, 19:27

Re: NXC array declaration

Post by ricardocrl »

afanofosc wrote: I have NOT extended the array initialization changes to 2+ dimensional arrays or to structures at this point.
This answered my couple of hours or more of debugging. :-)

I tried to look for the right information and end up here: http://bricxcc.sourceforge.net/nbc/nxcd ... rrays.html
It says:
Arrays of up to two dimensions may be initialized at the point of declaration...
Maybe this part should be removed (and the respective example).
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: NXC array declaration

Post by mattallen37 »

ricardocrl wrote:Maybe this part should be removed (and the respective example).
You can still initialize a multi dim array, but just not if you also specify the size. e.g. this wouldn't work:

Code: Select all

byte array[3][2] = {{23, 54},{36, 91},{82, 57}};
but this should be fine:

Code: Select all

byte array[][] = {{23, 54},{36, 91},{82, 57}};
Sry, I don't remember if I need commas between the sets of braces (compiles either way).
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: NXC array declaration

Post by afanofosc »

The commas are required for C and NXC really should complain about that. When I get around to extending the support for 2+ dimensions as described in this thread (i.e., supporting both a size specification and an initializer at the same time) then I will have it validate the format of the initializer and it will complain if it is missing those commas.

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

Who is online

Users browsing this forum: No registered users and 2 guests