1

I have structure declared like this:

TYPE board:
STRUCT
   number: INT;
   color: DWORD;
END_STRUCT
END_TYPE

And i want to declare array of these structures with starting values in POU. I do it like this:

Program PLC_PRG
VAR
   arr1: ARRAY[1..61] OF board;
   board: board;
   arr1[1].color := 16#FF0000;
END_VAR

But i get an error which say: "Error 4024: PLC_PRG(10): Expecting ':' before '['".

Do anybody know how to solve this problem?

kolyur
  • 457
  • 2
  • 13
koliber
  • 23
  • 4

1 Answers1

0

You didn't specify a platform but I'll assume TwinCAT. To initialize an array of structures, you have to do it all in the variable declaration line.

arr1: ARRAY[1..61] OF board := [(number:=7, color:=16#FF0000), (number:=5, color:=16#FF0001)];

This example will initialize elements 1 and 2 of the array. To my knowledge you can't selectively initialize individual array elements as you are maybe wanting to do. You could use statements like arr1[17].color := 16#FF0000 of course but it would have to be outside of your variable declaration block.

kolyur
  • 457
  • 2
  • 13
  • I am using Codesys so just removed square brackets. Do you know is there way to declare couple of elements the same time like this: arr2 : ARRAY [1..2,3..4] OF INT := [1,3(7)]; (* short for 1,7,7,7 *) but with array of structs? Because this don't work: arr1 : ARRAY[1..61] OF board := 6(number:=1, color:= 16#FF0000); – koliber Oct 29 '21 at 14:29
  • Square brackets are required in Codesys 3.5. This means you use CoDeSys 2.3. To give answer for multilevel array in a comment is hard. Create new question so that we can give you answer with examples. – Sergey Romanov Oct 31 '21 at 08:52
  • I don't know about CODESYS 2.3, but in 3.5 you should be able to initialize several identical objects with brackets. Alternatively, you may define a `init` variable and initialize your array once at the beginning of your program. This is especially useful if the array is big but can easily be initialized through a `FOR` loop! – Guiorgy Nov 01 '21 at 11:51