fortran在function中传递指针

本文深入探讨Fortran中指针函数的实现细节,包括如何通过interface声明指针函数,函数参数与返回值的正确使用,以及result子句在指针函数定义中的作用。文章还提供了具体的代码示例,展示如何在实际编程中操作指针数组。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值