fortran在function中传递指针

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

猜你喜欢

转载自blog.csdn.net/chd_lkl/article/details/83822980