Single-Dimensional Arrays

A single-dimensional array can be declared as shown:

INT[] intArray;
STRING[] stringArray;
FILE[] fileArray;

Array Access

Arrays can be accessed and assigned to as shown:

INT i, j;
INT[] array;
array[0] := 42;
array[10] := 99;
i := array[0];
j := array[10];
PRINT("array[0]: ", array[0], "\n");
PRINT("array[10]: ", array[10], "\n");
array[0]: 42
array[10]: 99

The array will grow in size if the index provided is not within the current bounds of the array.

Array Initialisation

Arrays can be initialised using curly brackets and commas to separate values like this:

INT[] i := { 1, 2, 42 };
INT[] j;
j := { 9, 72 }; // this sets j[0] to 9 and j[1] to 72.

Empty curly brackets generate an array of size zero:

INT[] i := {};
PRINT("i: ", WIDTHOF(i), "\n");
i: 0

Initialisation can reference other variables and arrays.

INT i := 42;
INT[] j := { i };
INT[] k := { j[0] };

Note: Initialisation outside of functions cannot reference other arrays.

Accessing Unset Elements

If an array element is accessed which has not been set, the default value for the type will be returned:

INT[] intArray;
STRING[] stringArray;
intArray[1] := 42;
stringArray[1] := "hello";
PRINT("int[0]: ", intArray[0], "\n");
PRINT("string[0]: ", stringArray[0], "\n");
int[0]: 0
string[0]: 

This is also true for indices beyond the bounds of the array:

INT[] array;
PRINT("array[99]: ", array[99], "\n");
array[99]: 0

Note that the array will grow when the index is beyond the bounds of the array:

INT[] array;
INT i;
i := array[99];
PRINT(WIDTHOF(array));
100

Assignment

Arrays are not passed by reference, so when an array is assigned to a variable, a copy is made:

INT[] array, array2;
array2 := array;
array[0] := 42;
PRINT("array2[0]: ", array2[0], "\n");
array2[0]: 0