ARRAYPermalink

One-, two-, and three-dimensional fields (arrays) are supported as elementary data types. Arrays can be defined both in the declaration part of a POU and in the global variable lists. Maximum 9 dimensions may result from nesting of arrays ( "ARRAY[0..2] OF ARRAY[0..3] OF …" ).

Syntax:

‎<Field_Name>:ARRAY [<ll1>..<ul1>,<ll2>..<ul2>] OF <elem. Type>.‎

ll1, ll2, ll3 identify the lower limit of the field range; ul1, ul2 and ul3 identify the upper limit. The limit values must be integers and must match the DINT range of values.

Example:

‎Card_game: ARRAY [1..13, 1..4] OF INT;‎

Initializing arrays:

Example for complete initialization of an array:

arr1 : ARRAY [1..5] OF INT := 1,2,3,4,5;
arr2 : ARRAY [1..2,3..4] OF INT := 1,3(7); (* short for 1,7,7,7 *)
arr3 : ARRAY [1..2,2..3,3..4] OF INT := 2(0),4(4),2,3; (* short for 0,0,4,4,4,4,2,3 *)

Example of the initialization of an array of a structure:

TYPE STRUCT1
STRUCT
p1:int;
p2:int;
p3:dword;
END_STRUCT
ARRAY[1..3] OF STRUCT1:= (p1:=1,p2:=10,p3:=4723),(p1:=2,p2:=0,p3:=299),(p1:=14,p2:=5,p3:=112);

Example of the partial initialization of an array:

‎arr1 : ARRAY [1..10] OF INT := 1,2;‎

Elements to which no value is pre-assigned are initialized with the default initial value of the basic type. In the example above, the elements anarray[6] to anarray[10] are therefore initialized with 0.

Accessing array components:

Array components are accessed in a two-dimensional array using the following syntax:

‎<Field_Name>[Index1,Index2]‎

Example:

‎Card_game [9,2]‎

If you define a function in your project with the name CheckBounds, you can use it to check for range overflows in your project.

⮫ “How is a project structured?”

⮫ “Function CheckBounds”