Array Variables

Arrays are a special type that allows a group of values to be kept under one variable name. The individual elements of the array are sequentially numbered. Each of the elements of the array can be used as a completely separate variable with its own independent value that may have its own type.

For example, assume that we would like to create an array called Data that had 50 elements. A single value inside of square brackets defines the size of the array, as follows:

Data[50];

Notice that since there is only one value inside of the square brackets, the array elements will always start numbering from 0, which would be referenced as Data[0]. The 5th array element would be Data[4]; the last (50th) element would be Data[49]. If simply defining the size of the array is not adequate, we can include two values inside of the square brackets, thereby defining the indices of the first and last elements of the array:

Data[10, 20];

The first element of the array would be referenced as Data[10]. Notice that in this case the 20 does not define the number of elements, but rather, the index of the last element, which would be referenced as Data[20], or the 11th and last array element.

When referring to a particular element of an array, the number enclosed in square brackets [ ] after the name Data is called the "subscript". An array that has arrays in its elements is defined as follows:

Data[5][10];

This declaration may be read as "Data is an array of 5 elements each of which is an array of 10 values." This is what is known as a multi-dimensioned array. Each dimension must appear as a number enclosed in square brackets [ ] that follows the variable name. Data has 2 dimensions; its actual number of values can be found by multiplying all of the dimensions together. This means that Data contains 50 values in 5 groups of 10. To refer to multi-dimensioned arrays, one subscript is required for each dimension (all arrays must have at least one dimension). The combination of subscripts indicates which element of the array is being referenced. For Data, the valid subscripts range from Data[0][0] to Data[4][9]. The order of the subscripts is significant and must correspond to the declaration. The number of dimensions is limited only by the amount of memory (RAM) available while the total array size must not exceed 64k of RAM.

Even with multiple dimensions, the starting and ending elements of each (dimension) may be exactly defined. The following declaration is for a 2 dimensional array whose valid elements range from Data[10][-1] to Data[20][6]:

Data[10, 20][-1, 6];

Arrays are most useful when the subscript is an expression or when used by a function which manipulates arrays such as Plot. As an expression, the subscript allows you to change which value is being referenced. For example, the expression for the subscript may be a variable; let's call it MotorState. If an array called MotorMessage is a text array with the following values set:

MotorMessage[0] = "Off";

MotorMessage[1] = "Starting";

MotorMessage[2] = "Running";

MotorMessage[3] = "Stopping";

MotorMessage[4] = "Alarm";

then the reference MotorMessage[MotorState] would have a different message depending upon the value of the variable MotorState. When MotorState is 0, the message would be "Off", and when it is 2, the message would be "Running". This message could be displayed on the screen so that when the value of the variable MotorState changed, the message would change accordingly.

For functions and statements that use arrays, the last dimension corresponds to the array used. For arrays with only one dimension, there is no ambiguity. For example, consider the statement that begins:

Plot(DataX[2][3], 10, ...

The values in the array DataX[2][3] to DataX[2][12] will be plotted. The last subscript is the only subscript that varies. Any preceding subscripts remain fixed for the function or statement. This rule means that it is not possible to directly plot the 10 values contained in DataX[0][0] to DataX[9][0], however, it is possible to plot DataX[0][0] to DataX[0][9]. This principal applies for all functions and statements that require arrays as parameters, but it only when there is more than one array dimension.

Topics in this section:

Mismatched Array Dimensions

Array Processing

Statically Declared vs. Dynamically Allocated Arrays

Automatic Pointer Dereferencing

Automatic Index Padding

Variable Declaration

Initialization during Declaration

Initialization in a Script

Variable Assignment

Assignment Involving Static Array Variables

Assignment Involving Dynamic Array Variables

Passing an Array to a Launched Module or Subroutine

Passing an Array to a Called Module

Passing an Array to a VTS Function

VTS Functions that Only Accept Dynamic Arrays

VTS Functions that Only Accept Array Elements