Colobot Wiki

An array is basically a collection of variables of the same type or class. You can use N-dimensional 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-dimensional array of floats
   (0, 1, 2, 3);   // an array literal

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

   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 elements [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 signaled 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 reference 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 actually modified.

See also[]

Categories, sizeof and CBOT Language