Fortran Coder

标题: 关于子程序的问题 [打印本页]

作者: lihu8918    时间: 2017-3-8 15:50
标题: 关于子程序的问题
本帖最后由 lihu8918 于 2017-3-8 15:52 编辑

以以下示例程序为例,为什么A和B数组shape不一样,但却可以在一块整体相加。
[Fortran] 纯文本查看 复制代码
module sub
  implicit none
  integer :: nx, ny
  real, allocatable :: B(:, :)
contains
  subroutine cal_u
    real :: A(0:nx+1, 0:ny+1)
    A = 1
    Call advect(A)
  end subroutine

  subroutine advect(AA)
    !real :: AA(:, :)
    real :: AA(0:nx+1, 0:ny+1)
    B = AA+1
  end subroutine
end module

program main
use sub
  nx=3
  ny=4
  allocate(B(nx, ny))
  B=1.0
  call cal_u
  write(*, *) B
end program


作者: vvt    时间: 2017-3-8 17:18
对于动态数组来说,编译器无法在编译时检查是否数组大小一致。
对于这种情况,是可以做整体计算的。此时,结果的大小以第一个数组为准。(当然我们并不推荐数组大小不一样的数组参与整体计算)




欢迎光临 Fortran Coder (http://bbs.fcode.cn/) Powered by Discuz! X3.2