fortran:如何将空指针传递给子例程,它将在其中定义并返回

fortran: how to pass a null pointer to a subroutine, in which it will be defined and returned

我想要一个小子例程来制作一个指针的副本,该指针已经分配/定义。我有以下测试代码:

implicit none
real ,pointer :: x(:),y(:)
real,target:: px
px=2.02
allocate(x(10))
x=20.0
call copy_1dptr(x,y)  !code aborting
write(6,*)'size x is ',size(x)
write(6,*)'y is ',y
end
subroutine copy_1dptr(x,y) 
real,pointer:: x(:),y(:)
allocate(y(size(x)))
y=x
end subroutine copy_1dptr

然而,(使用 ifort),当我 运行 它时,我总是得到中止错误,消息说:

forrtl: severe (408): fort: (7): Attempt to use pointer Y when it is not associated with a target

看来,空指针不能像我尝试做的那样用作实际的虚拟对象。真的吗?有什么解决办法吗。

指针参数需要显式接口。将子例程放在模块中或使其成为内部:

contains

  subroutine copy_1dptr(x,y) 
  end subroutine

end program