Fortran: Array of unknown size in type

前端 未结 1 764
抹茶落季
抹茶落季 2021-01-13 20:19

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 ::          


        
1条回答
  •  有刺的猬
    2021-01-13 20:23

    In Fortran 90-95:

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

    you must allocate 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 the arrays yourself using allocate(var%NBananasLeft(NBananaTypes))), but you avoid the possibility of memory leaks.

    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
    

    0 讨论(0)
提交回复
热议问题