Fortran: an array of unknown size in type

Perhaps this is a really stupid question, and you need to do it differently, but: Is it possible to have something like

type food INTEGER :: NBananasLeft(NBananaTypes) INTEGER :: NApplesLeft(NAppleTypes) end type food 

where are NBananaTypes and NAppleTypes unknown at compile time?

+4
source share
1 answer

In Fortran 90-95:

 type food INTEGER,pointer :: NBananasLeft(:) INTEGER,pointer :: NApplesLeft(:) end type food 

you must distribute the arrays yourself using allocate(var%NBananasLeft(NBananaTypes))) .

In Fortran 2003:

 type food INTEGER,allocatable :: NBananasLeft(:) INTEGER,allocatable :: NApplesLeft(:) end type food 

you must also allocate arrays yourself using allocate(var%NBananasLeft(NBananaTypes))) , but you avoid memory leaks.

In Fortran 2003, parameterized data types (only a few compilers support this):

 type food(NBananaTypes,NAppleTypes) integer,len :: NBananaTypes,NAppleTypes INTEGER :: NBananasLeft(NBananaTypes) INTEGER :: NApplesLeft(NAppleTypes) end type food 
+4
source

All Articles