欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

fortran在function中传递指针

程序员文章站 2022-03-09 09:57:54
...
Program test_ptrfunction
    implicit none
    interface  !.. function的参数为指针,所以要写interface或者将function写入到module中
        function fifth( ptr_array )
            integer, dimension(:), pointer :: fifth
            integer, dimension(:), pointer :: ptr_array
        end function
    end interface
    integer, target                :: a(5) = [1,2,3,4,5]
    integer, dimension(:), pointer :: ptr_array
    
    ptr_array => a 
    write( *,* ) fifth( ptr_array )  
    
End program test_ptrfunction
    
function fifth( ptr_array ) result( ptr_fifth )  !.. 函数返回指针,必须在函数定义中使用result子句。而且result变量必须声明为指针
    implicit none
    integer, dimension(:), pointer :: ptr_array
    integer, dimension(:), pointer :: ptr_fifth
    
    integer :: low
    integer :: high
    
    low  = lbound( ptr_array, 1 )
    high = ubound( ptr_array, 1 )
    ptr_fifth => ptr_array( low:high )
    
end function fifth

 

相关标签: fortran