将未定义大小的数组传递给子例程 [英] Passing an array of undefined size to a subroutine
问题描述
我正在尝试将一个未指定大小的数组传递给子程序,如下所示:
I am trying to pass an array of unspecified size to a subroutine like so
PROGRAM GOL
IMPLICIT NONE
INTEGER, PARAMETER :: size_x = 16, size_y = 16
LOGICAL, DIMENSION(1:size_x,1:size_y) :: universe
universe(:,:) = .FALSE.
CALL COUNT_NEIGHBOURS(universe, 1, 1)
END PROGRAM GOL
SUBROUTINE COUNT_NEIGHBOURS (universe, x, y)
LOGICAL, DIMENSION(:,:) :: universe
INTEGER :: x,y
!test
universe(x,y) = .TRUE.
RETURN
END SUBROUTINE COUNT_NEIGHBOURS
但是我得到错误from gfortran
However I get the error from gfortran
CALL COUNT_NEIGHBOURS(universe, 1, 1)
1
Error: Procedure 'count_neighbours' at (1) with assumed-shape dummy argument 'universe' must have an explicit interface
什么是正确的方法来做到这一点?
What is the correct way to do this?
推荐答案
正如错误消息中所述,编译器需要您的可调用过程的显式接口。 此答案显示了如何提供界面的三种方法。
在大多数情况下,将过程放在模块中或使其成为包含过程的首选方式,但是有时候,例如在需要更新时使用接口块(在下面的代码中)更容易一些旧的代码。
As described in the error message the compiler needs the explicit interface for your callable procedure. This answer shows three ways how to provide an interface.
In most cases the preferred way to put the procedure in a module or make it a contained procedure, but sometimes it's easier to use the interface block (in the code below) when, for example, you need to update some old code.
PROGRAM GOL
IMPLICIT NONE
interface
subroutine COUNT_NEIGHBOURS(universe, x, y)
logical :: universe(:,:)
integer :: x, y
end subroutine COUNT_NEIGHBOURS
end interface
INTEGER, PARAMETER :: size_x = 16, size_y = 16
LOGICAL, DIMENSION(1:size_x,1:size_y) :: universe
universe(:,:) = .FALSE.
CALL COUNT_NEIGHBOURS(universe, 1, 1)
END PROGRAM GOL
SUBROUTINE COUNT_NEIGHBOURS (universe, x, y)
LOGICAL :: universe(:,:)
INTEGER :: x,y
!test
universe(x,y) = .TRUE.
RETURN
END SUBROUTINE COUNT_NEIGHBOURS
这篇关于将未定义大小的数组传递给子例程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!