[問題] 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