看板 Fortran 關於我們 聯絡資訊
請問如何將指標傳入副程式內? 例如: 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)
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)
gilocustom:access violation應該是執行的時候才會顯示的訊息, 03/06 15:18
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)
gilocustom:deferred-shape array如ptr者,需要透過explicit inter 03/06 15:41
gilocustom:face才能夠傳遞。 03/06 15:41
gilocustom:找interface相關的篇章或是搜尋"fortran interface"就 03/06 15:48
gilocustom:有很多範例。 03/06 15:48
原來需要設定介面,困擾已久的問題。以解決。 感謝 gilocustom ※ 編輯: sin55688 來自: 140.113.22.204 (03/06 16:20)