Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Array initialized with fewer elements than expected

An array initializer provides fewer values than the declared array size. The remaining elements will be zero-initialized.

Example:

VAR
    arr : ARRAY[1..5] OF DINT := [1, 2, 3]; // only 3 of 5 elements
END_VAR

This is a warning because the compiler will implicitly fill the unspecified elements with their default (zero) value, which may or may not be intentional.

To silence this warning, provide all elements explicitly or use a multiplied initializer to fill the rest:

VAR
    arr : ARRAY[1..5] OF DINT := [1, 2, 3, 0, 0];   // explicit
    arr2 : ARRAY[1..5] OF DINT := [1, 2, 3, 2(0)];   // multiplied fill
END_VAR