Declaring the length of an array with a variable

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

Declaring the length of an array with a variable

Post by mattallen37 »

This works:

Code: Select all

byte ArrayOne[];
byte ArrayTwoLength;
task main()
{
  ArrayTwoLength=ArrayLen(ArrayOne)-3;
  byte ArrayTwo[ArrayTwoLength];
}
But not this:

Code: Select all

byte ArrayOne[];
task main()
{
  byte ArrayTwoLength;
  ArrayTwoLength=ArrayLen(ArrayOne)-3;
  byte ArrayTwo[ArrayTwoLength];
}
Why does the variable that defines the length of an array need to be global? Is there any special reason, or is it just a bug?

I want to be able to do something like the following:

Code: Select all

byte ArrayOne[];
task main()
{
  byte ArrayTwoLength=ArrayLen(ArrayOne)-3;
  byte ArrayTwo[ArrayTwoLength];
}
where the variable is obviously local, and when it is defined, it is also assigned a value.

Of course, so far I haven't run into a situation where this is really a problem, as there is a workaround. I am just curious as to the reason it must be global.
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: Declaring the length of an array with a variable

Post by afanofosc »

I would not try to use a variable in the way that you are trying. Instead use ArrayInit(array, value, len_variable);

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: Declaring the length of an array with a variable

Post by mattallen37 »

So, does this declare an array, or just resize it? What is the "value" parameter for?
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Declaring the length of an array with a variable

Post by HaWe »

mostly zero, 42, or your lucky numbers :)
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Declaring the length of an array with a variable

Post by mattallen37 »

doc-helmut wrote:mostly zero, 42, or your lucky numbers :)
Actually, that would be 0b101010 (or %101010) :lol:

Based on the help files, it looks like it is the value that it gives to all the elements of the array. Is that right?
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Declaring the length of an array with a variable

Post by HaWe »

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

Re: Declaring the length of an array with a variable

Post by muntoo »

mattallen37 wrote:Based on the help files, it looks like it is the value that it gives to all the elements of the array. Is that right?
It's true as much as the answer to the life the universe and everything is 42.

BTW, just something completely unrelated I thought you guys/gals/aliens/robots/von Neumann machines/etc might find interesting:
Douglas Adams wrote: Douglas Adams was asked many times during his career why he chose the number 42. Many theories were proposed,[6] but he rejected them all. On November 3, 1993, he gave an answer[7] on alt.fan.douglas-adams:
“ The answer to this is very simple. It was a joke. It had to be a number, an ordinary, smallish number, and I chose that one. Binary representations, base thirteen, Tibetan monks are all complete nonsense. I sat at my desk, stared into the garden and thought '42 will do'. I typed it out. End of story. ”
Image

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


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Declaring the length of an array with a variable

Post by afanofosc »

Yes, the value is the thing that gets copied into all len_variable elements in the newly reallocated array. If it is an array of structures or an array of strings or an array of arrays of int or whatever then the value should be of the appropriate type. I.e., do not ArrayInit an array of LocationType structs with the constant 0 or an empty string or some other kind of struct. That would be a bad thing to do.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: Declaring the length of an array with a variable

Post by muntoo »

Yeah, I've started using this whenever I want to initialize a LocationType (or whatever struct/etc):

Code: Select all

LocationType ltOut[];
LocationType ltTemp;
ltTemp.X = 0;
ltTemp.Y = 0;
ArrayInit(ltOut, ltTemp, 42);
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 17 guests