P4085

Code

P4085

Message

Number of subscripts does not match the number of array dimensions

This error occurs when a subscript list gives a number of subscripts other than the number of dimensions of the array it subscripts.

A subscript list selects an element of one array, with one subscript for each dimension, separated by commas. An element of a multi-dimensional array takes one list (grid[2, 3]). An element of an array of arrays takes one list for each array (rows[2][3]), because each list selects from one array.

Example

The following code will generate error P4085 twice:

TYPE
  Row : ARRAY[1..3] OF INT;
END_TYPE

PROGRAM main
  VAR
    grid : ARRAY[1..2, 1..3] OF INT;
    rows : ARRAY[1..2] OF Row;
  END_VAR
  grid[2][3] := 7;  (* Error: grid has two dimensions *)
  rows[2, 3] := 7;  (* Error: rows has one dimension *)
END_PROGRAM

grid is one array with two dimensions, so grid[2] gives too few subscripts. rows is an array of one dimension whose elements are arrays, so rows[2, 3] gives too many.

To fix this error, give each array one subscript for each of its dimensions:

TYPE
  Row : ARRAY[1..3] OF INT;
END_TYPE

PROGRAM main
  VAR
    grid : ARRAY[1..2, 1..3] OF INT;
    rows : ARRAY[1..2] OF Row;
  END_VAR
  grid[2, 3] := 7;
  rows[2][3] := 7;
END_PROGRAM

See Also

  • Array Types — multi-dimensional arrays and arrays of arrays

  • P2027 — a subscript outside the bounds of its dimension

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.