<variable name> : ARRAY[ <1st dimension> , <next dimensions> ] OF <data type> := <initialization> ;
<variable name> [ <index of 1st dimension> , <index of next dimensions> ]
|
First dimension (lower to higher index limit) |
|
The next dimensions (comma-separated) |
Syntax for data access
|
Index limit of the first dimension |
|
Index limit of the next dimensions |
Two-dimensional array
VAR aiCardGame : ARRAY[1..2, 3..4] OF INT; END_VAR
1st dimension: 1 to 2 (2 array elements)
2nd dimension: 3 to 4 (2 array elements)
Initialization for the 2 * 2 elements:
aiCardGame : ARRAY[1..2, 3..4] OF INT := [2(10),2(20)]; // Short notation for [10, 10, 20, 20]
Data access
iLocal_1 := aiCardGame[1, 3]; // Assignment of 10 iLocal_2 := aiCardGame[2, 4]; // Assignment of 20
Three-dimensional array
VAR aiCardGame : ARRAY[1..2, 3..4, 5..6] OF INT; END_VAR
1st dimension: 1 to 2
2nd dimension: 3 to 4
3rd dimension: 5 to 6
2 * 2 * 2 = 8 array elements
Initialization
aiCardGame : ARRAY[1..2, 3..4, 5..6] OF INT := [10, 20, 30, 40, 50, 60, 70, 80];
Data access
iLocal_1 := aiCardGame[1, 3, 5]; // Assignment of 10 iLocal_2 := aiCardGame[2, 3, 5]; // Assignment of 20 iLocal_3 := aiCardGame[1, 4, 5]; // Assignment of 30 iLocal_4 := aiCardGame[2, 4, 5]; // Assignment of 40 iLocal_5 := aiCardGame[1, 3, 6]; // Assignment of 50 iLocal_6 := aiCardGame[2, 3, 6]; // Assignment of 60 iLocal_7 := aiCardGame[1, 4, 6]; // Assignment of 70 iLocal_8 := aiCardGame[2, 4, 6]; // Assignment of 80
Initialization
aiCardGame : 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
iLocal_1 := aiCardGame[1, 3, 5]; // Assignment of 10 iLocal_2 := aiCardGame[2, 3, 5]; // Assignment of 10 iLocal_3 := aiCardGame[1, 4, 5]; // Assignment of 20 iLocal_4 := aiCardGame[2, 4, 5]; // Assignment of 20 iLocal_5 := aiCardGame[1, 3, 6]; // Assignment of 30 iLocal_6 := aiCardGame[2, 3, 6]; // Assignment of 30 iLocal_7 := aiCardGame[1, 4, 6]; // Assignment of 40 iLocal_8 := aiCardGame[2, 4, 6]; // Assignment of 40
Three-dimensional array of a user-defined structure
TYPE DATA_A STRUCT iA_1 : INT; iA_2 : INT; dwA_3 : DWORD; END_STRUCT END_TYPE PROGRAM PLC_PRG VAR aData_A : ARRAY[1..3, 1..3, 1..10] OF DATA_A; END_VAR
The array aData_A
consists of a total of 3 * 3 * 10 = 90 array elements of data type DATA_A
.
Initialize partially
aData_A : ARRAY[1..3, 1..3, 1..10] OF DATA_A := [(iA_1 := 1, iA_2 := 10, dwA_3 := 16#00FF),(iA_1 := 2, iA_2 := 20, dwA_3 := 16#FF00),(iA_1 := 3, iA_2 := 30, dwA_3 := 16#FFFF)];
In the example, only the first 3 elements are initialized explicitly. Elements to
which no initialization value is assigned explicitly are initialized internally with
the default value of the basic data type. This initializes the structure components
at 0 starting with the element aData_A[2, 1, 1]
.
Data access
iLocal_1 := aData_A[1,1,1].iA_1; // Assignment of 1 dwLocal_2 := aData_A[3,1,1].dwA_3; // Assignment of 16#FFFF
Array of a function block
FUNCTION BLOCK FBObject_A VAR iCounter : INT; END_VAR ... ; PROGRAM PLC_PRG VAR aObject_A : ARRAY[1..4] OF FBObject_A; END_VAR
The array aObject_A
consists of 4 elements. Each element instantiates a FBObject_A
function block.
Function call
aObject_A[2]();
Two-dimensional array of a function block
Implementation of FB_Something
with method FB_Init
FUNCTION_BLOCK FB_Something VAR _nId : INT; _lrIn : LREAL; END_VAR ... METHOD FB_Init : BOOL VAR_INPUT bInitRetains : BOOL; bInCopyCode : BOOL; nId : INT; lrIn : LREAL; END_VAR _nId := nId; _lrIn := lrIN;
The function block FB_Something
has a method FB_Init
that requires 2 parameters.
Instantiation of the array with initialization
PROGRAM PLC_PRG VAR fb_Something_1 : FB_Something(nId := 11, lrIn := 33.44); a_Something : ARRAY[0..1, 0..1] OF FB_Something[(nId := 12, lrIn := 11.22), (nId := 13, lrIn := 22.33), (nId := 14, lrIn := 33.55),(nId := 15, lrIn := 11.22)]; END_VAR
Note the capability of using the implicit monitoring function CheckBounds()
to monitor the maintenance of the index limits at runtime.