An integer variable declared in the module is used as a global variable to define the size of related arrays in the program. The size of program varies, so the size of array
An array declared like a(n)
is an explicit shape array. When n
is not a constant (named or otherwise, strictly a constant expression) such an array is an automatic object.
Automatic objects are restricted in where they may appear. In particular, an explicit shape array is subject to the following constraint (C531 of F2008):
An explicit-shape-spec whose bounds are not constant expressions shall appear only in a subprogram, derived type definition, BLOCK construct, or interface body.
As n
from the module mod
is not a constant it cannot be used as bounds of an array in the main program. The subroutine sub
is a subprogram and so a(n)
is a valid use of a non-constant bound.
Instead of having an automatic object in the main program, one can instead consider deferred shape arrays, using either pointer
or allocatable
attributes.