Page 1 of 1

Declaring the length of an array with a variable

Posted: 25 Feb 2011, 02:50
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.

Re: Declaring the length of an array with a variable

Posted: 25 Feb 2011, 03:24
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

Re: Declaring the length of an array with a variable

Posted: 25 Feb 2011, 03:30
by mattallen37
So, does this declare an array, or just resize it? What is the "value" parameter for?

Re: Declaring the length of an array with a variable

Posted: 25 Feb 2011, 08:24
by HaWe
mostly zero, 42, or your lucky numbers :)

Re: Declaring the length of an array with a variable

Posted: 25 Feb 2011, 08:30
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?

Re: Declaring the length of an array with a variable

Posted: 25 Feb 2011, 08:31
by HaWe
yes, IMHO :)

Re: Declaring the length of an array with a variable

Posted: 26 Feb 2011, 05:09
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. ”

Re: Declaring the length of an array with a variable

Posted: 26 Feb 2011, 05:35
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

Re: Declaring the length of an array with a variable

Posted: 27 Feb 2011, 03:53
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);