P4097¶
- Code
P4097
- Message
Subscripted variable is not an array
This error occurs when a subscript list (x[1]) is written on a variable
that is not an array. Only an array has elements for a subscript to select.
The variables this rejects include:
an elementary value, such as a
DINT, aWORDor aBOOLa
STRINGor aWSTRINGa structure or a function block instance, and a field of one when the field is not itself an array
an element of an array that is not itself an array, such as
values[1][2]on a one-dimensionalvaluesa reference to something that is not an array, with or without
^
A subscript of a reference to an array selects an element of the array it
refers to, so p[1] on a REF_TO ARRAY[1..3] OF DINT is accepted.
Example¶
The following code will generate error P4097 twice:
TYPE
Reading : STRUCT
level : DINT;
END_STRUCT;
END_TYPE
PROGRAM main
VAR
count : DINT;
sensor : Reading;
END_VAR
count[1] := 5; (* Error: count is a DINT *)
count := sensor.level[1]; (* Error: the field level is a DINT *)
END_PROGRAM
count and the field level each hold one value, so there is no element
for a subscript to select.
To fix this error, declare the variable or the field as an array, or remove the subscript:
TYPE
Reading : STRUCT
level : ARRAY[1..4] OF DINT;
END_STRUCT;
END_TYPE
PROGRAM main
VAR
counts : ARRAY[1..4] OF DINT;
sensor : Reading;
END_VAR
counts[1] := 5;
counts[2] := sensor.level[1];
END_PROGRAM
See Also¶
Array Types — declaring and subscripting an array
P4085 — a subscript list with the wrong number of subscripts
Think IronPLC is wrong about this?
If you believe this diagnostic is incorrect, open an issue on GitHub with a small sample that demonstrates the problem.