在下面的程序中,提供了两种传递数组的方法:
program main
integer, dimension(4) :: x = [9, 8, 7, 6]
call print_x(x(2:3)) ! Method 1
call print_x(x(2)) ! Method 2
end program
subroutine print_x(x)
integer, dimension(2), intent(in) :: x
print *, x
end subroutine两种方法产生相同的结果:打印数字8和7。就我个人而言,我永远不会使用方法2进行编码,因为它看起来像是传递的是单个值,而不是一个数组。
你能举例说明什么时候必须使用方法2而不是方法1吗?
发布于 2019-07-17 04:23:09
考虑一下这个程序
implicit none
integer :: x(2,2)=0
call set(x(2,1))
print*, x
contains
subroutine set(y)
integer y(2)
y = [1,2]
end subroutine set
end program此子例程调用中的伪参数y是与元素x(2,1)和x(1,2)关联的参数。x中没有包含这两个元素的数组部分。
https://stackoverflow.com/questions/57063755
复制相似问题