Array with fixed length

Arrays can be declared in the declaration part of a POU or in global variable lists.

Syntax for declaring a one-dimensional array

<variable name> : ARRAY[ <dimension> ] OF <data type> ( := <initialization> )? ;
<dimension> : <lower index bound>..<upper index bound>
<data type> : elementary data types | user defined data types | function block types
// (...)? : Optional

Syntax for declaring a multidimensional array

<variable name> : ARRAY[ <1st dimension> ( , <next dimension> )+ ] OF <data type> ( := <initialization> )? ;
<1st dimension> : <1st lower index bound>..<1st upper index bound>
<next dimension> : <next lower index bound>..<next upper index bound>
<data type> : elementary data types | user defined data types | function block types
// (...)+ : One or more further dimensions
// (...)? : Optional

The index limits are integer numbers up to data type DINT.

Syntax for data access

<variable name>[ <index of 1st dimension> ( , <index of next dimension> )* ]
// (...)* : 0, one or more further dimensions
Array with fixed length 1:

Note the option to monitor violation of field bounds at runtime using the implicit monitoring function CheckBounds.

See also:

Array with fixed length 2:

Setting the monitoring range for large arrays

If an array has more than 1000 elements, then 1000 elements are displayed by default in the online view. You can change this monitoring range by double-clicking the array declaration in the declaration section in the online view. A dialog then opens in which you can set the start and end index of the desired monitoring range.

Please note that the display performance is reduced, the more elements are simultaneously displayed (maximum 20000 elements).

Example: One-dimensional array

Declaration 1:

One-dimensional array of 10 integer elements

Lower index limit: 0
Upper index limit: 9

VAR
    aCounter : ARRAY[0..9] OF INT;
END_VAR

Initialization 1:

aCounter : ARRAY[0..9] OF INT := [0, 10, 20, 30, 40, 50, 60, 70, 80, 90];

Data access 1:

The local variable is assigned the value 20.

nLocalVariable := aCounter[2];

Declaration 2:

VAR CONSTANT
    cMin    : INT := 0;
    cMax    : INT := 5;
END_VAR
VAR
    aSample : ARRAY [cMin..cMax] OF BOOL;
END_VAR

Data access 2:

The array is accessed by means of an index variable. Before the array is accessed, the system checks whether the value of the index variable is within the valid array boundaries.

IF nIndex >= cMin AND nIndex <= cMax THEN
    bValue := aSample[nIndex];
END_IF

Example: Two-dimensional array

Declaration:

First dimension: 1 to 2
Second dimension: 3 to 4

VAR
    aCardGame : ARRAY[1..2, 3..4] OF INT;
END_VAR

Initialization:

aCardGame : ARRAY[1..2, 3..4] OF INT := [2(10),2(20)]; // Short notation for [10, 10, 20, 20]

Data access:

nLocal1 := aCardGame[1, 3]; // Assignment of 10
nLocal2 := aCardGame[2, 4]; // Assignment of 20

Example: Three-dimensional array

Declaration:

First dimension: 1 to 2
Second dimension: 3 to 4
Third dimension: 5 to 6
2 * 2 * 2 = 8 array elements

VAR
    aCardGame : ARRAY[1..2, 3..4, 5..6] OF INT;
END_VAR

Initialization 1:

aCardGame : ARRAY[1..2, 3..4, 5..6] OF INT := [10, 20, 30, 40, 50, 60, 70, 80];

Data access 1:

nLocal1 := aCardGame[1, 3, 5]; // Assignment of 10
nLocal2 := aCardGame[2, 3, 5]; // Assignment of 20
nLocal3 := aCardGame[1, 4, 5]; // Assignment of 30
nLocal4 := aCardGame[2, 4, 5]; // Assignment of 40
nLocal5 := aCardGame[1, 3, 6]; // Assignment of 50
nLocal6 := aCardGame[2, 3, 6]; // Assignment of 60
nLocal7 := aCardGame[1, 4, 6]; // Assignment of 70
nLocal8 := aCardGame[2, 4, 6]; // Assignment of 80

Initialization 2:

aCardGame : ARRAY[1..2, 3..4, 5..6] OF INT := [2(10), 2(20), 2(30), 2(40)]; // Short notation for [10, 10, 20, 20, 30, 30, 40, 40]

Data access 2:

nLocal1 := aCardGame[1, 3, 5]; // Assignment of 10
nLocal2 := aCardGame[2, 3, 5]; // Assignment of 10
nLocal3 := aCardGame[1, 4, 5]; // Assignment of 20
nLocal4 := aCardGame[2, 4, 5]; // Assignment of 20
nLocal5 := aCardGame[1, 3, 6]; // Assignment of 30
nLocal6 := aCardGame[2, 3, 6]; // Assignment of 30
nLocal7 := aCardGame[1, 4, 6]; // Assignment of 40
nLocal8 := aCardGame[2, 4, 6]; // Assignment of 40

Example: Three-dimensional array of a user-defined structure

Declaration:

The array aData consists of a total of 3 * 3 * 10 = 90 array elements of data type ST_Data.

TYPE ST_Data
STRUCT
    n1 : INT;
    n2 : INT;
    n3 : DWORD;
END_STRUCT
END_TYPE
PROGRAM MAIN 
VAR
    aData : ARRAY[1..3, 1..3, 1..10] OF ST_Data;
END_VAR

Partial initialization:

In the example only the first 3 elements are explicitly initialized. Elements to which no initialization value is explicitly assigned are internally initialized with the default value of the basic data type. Thus the structure components are initialized with 0 starting from the element aData[2, 1, 1].

aData : ARRAY[1..3, 1..3, 1..10] OF ST_Data := [(n1 := 1, n2 := 10, n3 := 16#00FF),
                                                (n1 := 2, n2 := 20, n3 := 16#FF00),
                                                (n1 := 3, n2 := 30, n3 := 16#FFFF)];

Data access:

nLocal1 := aData[1,1,1].n1; // Assignment of 1
nLocal2 := aData[3,1,1].n3; // Assignment of 16#FFFF

Example: Array of a function block

Declaration:

The array aObjects consists of 4 elements. Each element instantiates a function block FB_Object.

FUNCTION BLOCK FB_Object
VAR
    nCounter : INT;
END_VAR
PROGRAM MAIN
VAR
    aObjects : ARRAY[1..4] OF FB_Object;
END_VAR

Function call:

aObjects[2]();

Example: Array of a function block with FB_init method

Declaration:

Implementation of FB_Sample with method FB_init

FUNCTION_BLOCK FB_Sample
VAR
    _nId : INT;
    _fIn : LREAL;
END_VAR

The function block FB_Sample has a method FB_init that requires two parameters.

METHOD FB_init : BOOL
VAR_INPUT
    bInitRetains : BOOL;
    bInCopyCode  : BOOL;
    nId          : INT;
    fIn          : LREAL;
END_VAR
_nId := nId;
_fIn := fIN;

Array declaration with initialization:

PROGRAM MAIN
VAR
    fbSample : FB_Sample (nId := 11, fIn := 33.44);
    aSample  : ARRAY[0..1, 0..1] OF FB_Sample [(nId := 12, fIn := 11.22),
                                               (nId := 13, fIn := 22.33),
                                               (nId := 14, fIn := 33.55),
                                               (nId := 15, fIn := 11.22)];
END_VAR