作者sin55688 (单手挑藏獒)
看板Fortran
标题[问题] pointer 传入 subroutine
时间Thu Mar 6 13:49:41 2014
请问如何将指标传入副程式内?
例如:
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
我目的是希望能在副程式内,宣告阵列大小并赋予值。
最後并回传到主程式,但无法成功。
尝试了一下发现副程式接收的好像是指标所指的东西,而非指标本身。
请教各位高手,这该如何改写,谢谢
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.113.22.204
※ 编辑: sin55688 来自: 140.113.22.204 (03/06 14:00)
1F:→ gilocustom:在编译这份范例的时候你用的编译器没有抱怨些什麽吗? 03/06 14:34
access violation
这应该跟我pointer尚未配置 或 subroutine 传入接收型态不一致有关
所以想请教,如何才能将未配置大小的阵列指标,传入副程式当中。
并在副程式内配置大小。
※ 编辑: sin55688 来自: 140.113.22.204 (03/06 14:49)
※ 编辑: sin55688 来自: 140.113.22.204 (03/06 15:06)
2F:→ gilocustom:access violation应该是执行的时候才会显示的讯息, 03/06 15:18
3F:→ gilocustom:缺少副程式sub的话不可能编译成功。 03/06 15:23
抱歉,这部分是打错了。已修改subroutine名称。
我尝试了其他方式
例如:
program main
integer, pointer :: ptr(:)
allocate( ptr(3))
ptr = (/1,2,3/)
call sub2(ptr)
end
subroutine sub2(p)
integer :: p(:)
write(*,*) p
end
如此是可以编译成功且执行的。
但这就不是我想要的结果,传入的东西变成指标所指向的整数阵列
而非我所希望的指标本身。
手边书本找不到相关的程式范例,希望能给点指教
※ 编辑: sin55688 来自: 140.113.22.204 (03/06 15:34)
4F:→ gilocustom:deferred-shape array如ptr者,需要透过explicit inter 03/06 15:41
5F:→ gilocustom:face才能够传递。 03/06 15:41
6F:→ gilocustom:找interface相关的篇章或是搜寻"fortran interface"就 03/06 15:48
7F:→ gilocustom:有很多范例。 03/06 15:48
原来需要设定介面,困扰已久的问题。以解决。
感谢 gilocustom
※ 编辑: sin55688 来自: 140.113.22.204 (03/06 16:20)