Fortran Coder

查看: 14134|回复: 6
打印 上一主题 下一主题

[Module] MODULE 中使用动态数组

[复制链接]

798

帖子

2

主题

0

精华

大宗师

F 币
3793 元
贡献
2268 点
楼主
发表于 2016-5-23 17:53:36 | 显示全部楼层
第一种,子程序和动态数组放在同一模块
[Fortran] 纯文本查看 复制代码
module m 
integer,allocatable::n(:)
contains 
! 分配
subroutine sub1
allocate(n(3))
end subroutine

! 赋值
subroutine sub2
n=[1,2,3]
end subroutine

! 使用
subroutine sub3
integer i
i=n(2)
print*,i
end subroutine

! 释放
subroutine sub4
deallocate(n)
end subroutine
end module 

program test
use m
call sub1
call sub2
call sub3
call sub4
end 

798

帖子

2

主题

0

精华

大宗师

F 币
3793 元
贡献
2268 点
沙发
发表于 2016-5-23 17:55:08 | 显示全部楼层
本帖最后由 li913 于 2016-5-23 17:56 编辑

第二种,子程序和数组不在同一模块
[Fortran] 纯文本查看 复制代码
module m 
integer,allocatable::n(:)
end module 

program test
call sub1
call sub2
call sub3
call sub4
end 
! 分配
subroutine sub1
use m
allocate(n(3))
end subroutine

! 赋值
subroutine sub2
use m
n=[1,2,3]
end subroutine

! 使用
subroutine sub3
use m
integer i
i=n(2)
print*,i
end subroutine

! 释放
subroutine sub4
use m
deallocate(n)
end subroutine


您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-5-6 21:32

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表