|
Post by bitvast on May 25, 2014 8:57:48 GMT 1
In C, you can do this:
int myArray[5] = {1,2,3,4,5};
And I believe something similar can be done in some versions of BASIC. It would be nice to have a quick and simple way of initialising small arrays in BaCon without having to write a loop or do
myArray[0] = 1 myArray[1] = 2 etc.
Is this a good idea for a new feature? Since you can already do it in C I don't suppose it would be too hard to implement, but I've no idea really. What do you think, Peter?
p.s. Not sure if you can initialise multidimensional arrays in C like this:
int myArray[3][3] = {{1,2,3},{4,5,6},{7,8,9}}
If so, it would be nice to be able to do it in BaCon too.
|
|
|
Post by vovchik on May 25, 2014 9:29:57 GMT 1
Dear bitvast, This has always been possible in BaCon: DECLARE adje$[] = { "autumn", "hidden", "bitter", "misty", "silent", "empty", "dry", "dark", "summer", "icy", \ "delicate", "quiet", "white", "cool", "spring", "winter", "patient", "twilight", "dawn", "crimson", \ "wispy", "weathered", "blue", "billowing", "broken", "cold", "damp", "falling", "frosty", "green", \ "long", "late", "lingering", "bold", "little", "morning", "muddy", "old", "red", "rough", \ "still", "small", "sparkling", "throbbing", "shy", "wandering", "withered", "wild", "black", "young", \ "holy", "solitary", "fragrant", "aged", "snowy", "proud", "floral", "restless", "divine", "polished", \ "ancient", "purple", "lively", "nameless" }
and you can get the size back with: DEF FN bound(x) = SIZEOF(x)/SIZEOF(STRING) x = bound(adje$)
I haven't tried this with multiple dimensions, but have a go... With kind regards, vovchik
|
|
|
Post by bitvast on May 25, 2014 10:23:02 GMT 1
Hey vovchik, thanks. Multidimensions don't seem to work, although maybe I have the syntax wrong.
|
|
|
Post by Pjot on May 25, 2014 10:50:22 GMT 1
Folks, Multidimensional initialization always worked, see my example from 2010 here. Another example: DECLARE two_dim[3][3] = { { 1, 1}, {2, 2}, {3, 3} } TYPE int
PRINT two_dim[0][1] PRINT two_dim[1][1] PRINT two_dim[2][1]
HTH Peter
|
|
|
Post by vovchik on May 25, 2014 11:27:57 GMT 1
Thanks, Peter!
|
|
|
Post by bitvast on May 25, 2014 12:22:39 GMT 1
Thanks Peter. Just playing around, if you dimension the array to a number greater than the number of elements initialised, the remaining elements are zero- DECLARE dim[10] = {1,2} TYPE int PRINT dim[1] PRINT dim[5]
Gives 2 and 0. But if you try to access an element beyond the dimension you still get zero- DECLARE dim[10] = {1,2} TYPE int PRINT dim[99] I thought this would have generated an error. Also, am I right in thinking that you cannot use OPTION BASE 1 with multiple dimensions? I get an error using it.
|
|
|
Post by Pjot on May 25, 2014 13:33:06 GMT 1
Hi bitvast, This is pure luck - it depends on the type of system, compiler, memory management etc. Like mentioned in the manual, BaCon is a lazy converter - it simply passes your commands to C and it is handled there. Similarly, this has to do with the way your system allocates space on the stack. It is pure luck that you can refer to an element behind the allocated range, though a memory detection program like valgrind will issue a warning for this. The OPTION BASE currently is implemented for either one-dimensional arrays, or for multi-dimensional arrays but without initialization. This is a heritage from the past where the array parsing was handled differently. I will look into it again to see if this can be changed. HTH, Peter
|
|
|
Post by bitvast on May 25, 2014 15:12:20 GMT 1
Message understood. So the moral is: don't play fast and loose with initializing arrays. Thanks again, Peter.
|
|
|
Post by bigbass on May 25, 2014 18:36:02 GMT 1
|
|
|
Post by vovchik on May 25, 2014 18:56:13 GMT 1
Dear Joe, Thanks...It's a useful table. With kind regards, vovchik
|
|