Arrays
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.
Syntax:
<Field_Name>:ARRAY
[<LowLim1>..<UpLim1>, <LowLim2>..<UpLim2>]
OF <elem. Type>
LowLim1, LowLim2 identify the lower limit of the field range; UpLim1 and UpLim2 identify the upper limit. The range values must be integers.
Example:
Card_game: ARRAY [1..13, 1..4] OF INT;
Initializing of Arrays
You can initialize either all of the elements in an array or none of them.
Example for initializing arrays:
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 for the initialization of an array of a structure:
TYPE STRUCT1
STRUCT
p1:int;
p2:int;
p3:dword;
END_STRUCT
arr1 : 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 arr1[3] to arr1[10] are therefore initialized with 0.
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 automatically check for out-of-range errors in arrays ! The name of the function is fixed and can only have this designation. |