Fandom

Colobot Wiki

Array

67pages on
this wiki
Add New Page
Comments0 Share

An array is basically a collection of variables of the same type or class. You can use N-dimensionnal arrays in the CBOT language. Each dimension is limited to 9999 elements. You must use square brackets [] after the type name or the variable name to declare an array.

   int    [ ] a;   // an array of int
   int    a [12];  // an array of int limited to 12 elements
   string s[3];    // an array of 3 strings
   float  xy[][];  // a 2-dimensionnal array of floats
   (0, 1, 2, 3);   // an array literal

Actually when the CBOT interpreter encounters an array declaration, it just creates a null pointer:

   int a[5];  // a is now a null reference

As soon as you put values into the array, the elements are created and the reference is initialized:

   a[2] = 213;  // a points to
                // 3 élements [0], [1] et [2]

After this operation, a contains a reference to the elements of the array. Elements [0] and [1] are created but not initialized because an array cannot contain empty elements. The sizeof instruction allows you to obtain the number of elements contained in an array.

When an array is declared with a maximum size, the program will stop as soon as there is an access beyond the maximum array size. No error is signalled during compilation even if the error is obvious:

   {
       int a[5];
       a[7] = 123;  // no error at compile time
                    // but error at run time
   }

If you pass an array as parameter to a function, the function only receives a pointer to the array. That means if you modify an array element in the function, the element of the array that has been passed the function will be actuallay modified.

See alsoEdit

Categories, sizeof and CBOT Language

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.