NXC array declaration
-
- Posts: 1818
- Joined: 02 Oct 2010, 02:19
- Location: Michigan USA
- Contact:
NXC array declaration
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).
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
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
Re: NXC array declaration
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
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/
http://bricxcc.sourceforge.net/
-
- Posts: 1818
- Joined: 02 Oct 2010, 02:19
- Location: Michigan USA
- Contact:
Re: NXC array declaration
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
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
Re: NXC array declaration
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
John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
http://bricxcc.sourceforge.net/
-
- Posts: 1818
- Joined: 02 Oct 2010, 02:19
- Location: Michigan USA
- Contact:
Re: NXC array declaration
Thanks for the effort!
Matt
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
Re: NXC array declaration
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
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/
http://bricxcc.sourceforge.net/
-
- Posts: 1818
- Joined: 02 Oct 2010, 02:19
- Location: Michigan USA
- Contact:
Re: NXC array declaration
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
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
-
- Posts: 117
- Joined: 27 Dec 2010, 19:27
Re: NXC array declaration
This answered my couple of hours or more of debugging.afanofosc wrote: I have NOT extended the array initialization changes to 2+ dimensional arrays or to structures at this point.
I tried to look for the right information and end up here: http://bricxcc.sourceforge.net/nbc/nxcd ... rrays.html
It says:
Maybe this part should be removed (and the respective example).Arrays of up to two dimensions may be initialized at the point of declaration...
-
- Posts: 1818
- Joined: 02 Oct 2010, 02:19
- Location: Michigan USA
- Contact:
Re: NXC array declaration
You can still initialize a multi dim array, but just not if you also specify the size. e.g. this wouldn't work:ricardocrl wrote:Maybe this part should be removed (and the respective example).
Code: Select all
byte array[3][2] = {{23, 54},{36, 91},{82, 57}};
Code: Select all
byte array[][] = {{23, 54},{36, 91},{82, 57}};
Matt
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
Re: NXC array declaration
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
John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
http://bricxcc.sourceforge.net/
Who is online
Users browsing this forum: No registered users and 2 guests