[问题] pointer 传入 subroutine

楼主: sin55688 (单手挑藏獒)   2014-03-06 13:49:41
请问如何将指标传入副程式内?
例如:
program main
implicit none
integer, pointer :: ptr(:)
call sub(ptr)
write(*,*) ptr
end
subroutine sub(ptr)
implicit none
integer, pointer :: ptr(:)
allocate( ptr(3) )
ptr = (/1,2,3/)
end
我目的是希望能在副程式内,宣告阵列大小并赋予值。
最后并回传到主程式,但无法成功。
尝试了一下发现副程式接收的好像是指标所指的东西,而非指标本身。
请教各位高手,这该如何改写,谢谢
作者: gilocustom   2014-03-06 14:34:00
在编译这份范例的时候你用的编译器没有抱怨些什么吗?access violation应该是执行的时候才会显示的讯息,缺少副程式sub的话不可能编译成功。deferred-shape array如ptr者,需要透过explicit interface才能够传递。找interface相关的篇章或是搜寻"fortran interface"就有很多范例。

Links booklink

Contact Us: admin [ a t ] ucptt.com