[問題] pointer 傳入 subroutine

看板Fortran作者 (單手挑藏獒)時間10年前 (2014/03/06 13:49), 編輯推噓0(007)
留言7則, 1人參與, 最新討論串1/1
請問如何將指標傳入副程式內? 例如: 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)

03/06 14:34, , 1F
在編譯這份範例的時候你用的編譯器沒有抱怨些什麼嗎?
03/06 14:34, 1F
access violation 這應該跟我pointer尚未配置 或 subroutine 傳入接收型態不一致有關 所以想請教,如何才能將未配置大小的陣列指標,傳入副程式當中。 並在副程式內配置大小。 ※ 編輯: sin55688 來自: 140.113.22.204 (03/06 14:49) ※ 編輯: sin55688 來自: 140.113.22.204 (03/06 15:06)

03/06 15:18, , 2F
access violation應該是執行的時候才會顯示的訊息,
03/06 15:18, 2F

03/06 15:23, , 3F
缺少副程式sub的話不可能編譯成功。
03/06 15:23, 3F
抱歉,這部分是打錯了。已修改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)

03/06 15:41, , 4F
deferred-shape array如ptr者,需要透過explicit inter
03/06 15:41, 4F

03/06 15:41, , 5F
face才能夠傳遞。
03/06 15:41, 5F

03/06 15:48, , 6F
找interface相關的篇章或是搜尋"fortran interface"就
03/06 15:48, 6F

03/06 15:48, , 7F
有很多範例。
03/06 15:48, 7F
原來需要設定介面,困擾已久的問題。以解決。 感謝 gilocustom ※ 編輯: sin55688 來自: 140.113.22.204 (03/06 16:20)
文章代碼(AID): #1J60nt5y (Fortran)