Fortran语言:类型未知大小的数组 [英] Fortran: Array of unknown size in type
问题描述
也许,这是一个非常愚蠢的问题,应该真正做到这一点不同,但是:
是否有可能有类似
Perhaps this is a really stupid question and one should really do this differently, but: Is there a possibility to have something like
type food
INTEGER :: NBananasLeft(NBananaTypes)
INTEGER :: NApplesLeft(NAppleTypes)
end type food
在这里NBananaTypes和NAppleTypes不是在编译时知道的?
where NBananaTypes and NAppleTypes is not known at compilation time?
推荐答案
在Fortran语言90-95:
In Fortran 90-95:
type food
INTEGER,pointer :: NBananasLeft(:)
INTEGER,pointer :: NApplesLeft(:)
end type food
您必须分配自己使用的阵列分配(VAR%NBananasLeft(NBananaTypes)))
。
you must allocate the arrays yourself using allocate(var%NBananasLeft(NBananaTypes)))
.
在2003 Fortran语言:
In Fortran 2003:
type food
INTEGER,allocatable :: NBananasLeft(:)
INTEGER,allocatable :: NApplesLeft(:)
end type food
您还必须分配自己使用分配阵列(VAR%NBananasLeft(NBananaTypes)))
,但你避免内存泄漏的可能性。
you must also allocate the arrays yourself using allocate(var%NBananasLeft(NBananaTypes)))
, but you avoid the possibility of memory leaks.
在2003 Fortran语言通过参数化的数据类型(只有少数编译器支持):
In Fortran 2003 by parametrized data types (only a few compilers support that):
type food(NBananaTypes,NAppleTypes)
integer,len :: NBananaTypes,NAppleTypes
INTEGER :: NBananasLeft(NBananaTypes)
INTEGER :: NApplesLeft(NAppleTypes)
end type food
这篇关于Fortran语言:类型未知大小的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!