Page 1 of 2
NXC array declaration
Posted: 24 Aug 2012, 16:11
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).
Re: NXC array declaration
Posted: 24 Aug 2012, 17:58
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
Re: NXC array declaration
Posted: 24 Aug 2012, 18:21
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.
Re: NXC array declaration
Posted: 27 Aug 2012, 14:32
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
Re: NXC array declaration
Posted: 27 Aug 2012, 17:19
by mattallen37
Thanks for the effort!
Re: NXC array declaration
Posted: 12 Sep 2012, 06:53
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
Re: NXC array declaration
Posted: 12 Sep 2012, 17:10
by mattallen37
Thanks, downloading test release now
Re: NXC array declaration
Posted: 30 Sep 2012, 20:17
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).
Re: NXC array declaration
Posted: 01 Oct 2012, 03:18
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).
Re: NXC array declaration
Posted: 01 Oct 2012, 17:27
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