|
ARRAY |
|
|
An array is used to "group" a number of data elements together so that it can be referenced by an index.
An array can also be initialized - the exception being arrays declared in VAR_INPUT / VAR_OUTPUT or in a FUNCTION. In case an array is not fully initialized, the remaining elements in the array will simply be set to the default value 0 - FALSE by the compiler.
VPL supports one-dimensional and two-dimensional arrays with a maximum number of elements defined by the execution architecture. A one-dimensional array is declared as ARRAY [low..high] OF type. A two-dimensional array is declared as ARRAY [low1..high1, low2..high2] OF type. Arrays declared as global VAR_INPUT or VAR_OUTPUT variables can only be one-dimensional. Array bounds are inclusive and indexes must not be negative. A two-dimensional array is indexed by using both indexes, for example Matrix[2,3]. For two-dimensional arrays the second index changes fastest. Initial values are assigned in row-major order, so ARRAY [1..2, 1..3] receives values for [1,1], [1,2], [1,3], [2,1], [2,2], and [2,3]. A two-dimensional array can also be initialized using bracketed rows, for example ARRAY [1..2, 1..3] OF INT := [1, 2, 3], [4, 5, 6]. Each bracketed row may contain at most the number of values in the second dimension. Short rows are padded with default values before the next row starts. If fewer rows are supplied than the first dimension allows, the remaining elements are also initialized to the default value. The total number of elements in a two-dimensional array is the size of the first dimension multiplied by the size of the second dimension. SIZEOF(ArrayName) returns the total storage size of the array, while SIZEOF(ArrayName[i,j]) returns the size of one element. Under X32/NX32 the maximum total number of elements is 65535 and the maximum size of each element is also 65535. Under the NX32L execution the actual number and size of the defined arrays are limited by the available memory.
Array assignmentVPL does not support assignment of one complete standalone ARRAY variable to another. Individual array elements can be assigned normally. When the element type is a STRUCT_BLOCK, the source and destination elements must have the exact same STRUCT_BLOCK type. destination[i] := source[j]; Arrays declared as members of a STRUCT_BLOCK are copied automatically when the containing STRUCT_BLOCK is assigned. See STRUCT_BLOCK for the complete assignment rules.
Arrays can be supplied to functions by access using an ACCESS ARRAY parameter in the function VAR_INPUT section. The function receives direct access to the caller's array storage; the array is not copied. For one-dimensional ACCESS ARRAY parameters, a larger actual array is accepted when its bounds fully cover the formal bounds. For two-dimensional ACCESS ARRAY parameters, dimensions and bounds must match exactly.
ACCESS ARRAY function parameter example: FUNCTION ClearRange : INT;
One-dimensional array Example: INCLUDE rtcu.inc
Two-dimensional array example: VAR |