Module or main program array must have constant shape error in Fortran

Viewed 4870

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 is a variable but not a parameter. It is determined at the beginning of the program.

In the following snippet of code, n is the global size variable. It is declared in the module and defined at the beginning of main function/program. Similar usage of n in the main program and the subroutine contained in the main program to initialise an array respectively. However, the initialisation in the main program causes the error: module or main program array must have constant shape error, but the initialisation in subroutine works. What is the mechanism behind this different treatment of non-constant values used in different positions?

module mod
  implicit none
  integer :: n
end module mod



program main
  use mod
  implicit none
  integer :: b(n)
  n = 5
  b(:) = 1
  print*, b(:)

  call sub

contains

  subroutine sub
    integer :: a(n)
    a = 10
    print*, a
  end subroutine sub

end program main
1 Answers
Related