Gonzague
Gonzague

Reputation: 39

Fortran: Type containing a member that is an array of elements of this type

I need to declare a type a, containing a member that is an array of elements of type a.

I am thinking about something like:

type:: a
type(pta), dimension(:), allocatable:: array
end type a

type:: pta
class(a), pointer:: p
end type pta  

What is the right way to do that in Fortran ?

Upvotes: 1

Views: 77

Answers (2)

High Performance Mark
High Performance Mark

Reputation: 78364

Or get yourself a Fortran 2008 compiler and you can write

type :: a
  type(a), dimension(:), allocatable :: array
end type

While appreciating the rep garnered by the original form of this answer I should point out that, as far as I know, only the IBM and Cray Fortran compilers currently support this feature of the emerging standard. @Stefan's answer is implementable on all the current crop of widely used Fortran compilers.

Upvotes: 4

Stefan
Stefan

Reputation: 2518

You can simply insert a pointer in your type. This would look like:

type a
  type(a), dimension(:), pointer :: array
end type

You can then simply allocate the array in its desired size.

Upvotes: 2

Related Questions