Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28996 Discussions

Defining an array of pointers into Type.

qing83
Beginner
280 Views

I would like to define an array of pointers into Type.

type names

real :: p

type (list), dimension(:), pointer :: pL

end type names

type list

integer :: a

end type list

type(list), dimension(:), pointer :: pB

Then I have allocatedpB by:

nullify( pB )

allocate( pB(100) )

I need now to give dimension to pL and point it to some values of pB. How can I do this.

Thanks in advance.

Qing

0 Kudos
1 Reply
jimdempseyatthecove
Honored Contributor III
280 Views

>> I would like to define an array of pointers into Type

I find it helps to declare your types inside out.
i.e. from inner most level

! define your type
type YourType
! Insert your type fields here
integer :: ID! sample field for illustration
...
end type YourType

! define a pointer to your type
type ptrYourType
type(YourType), pointer :: pT
end type ptrYourType

! declare a list of pointers to your type
type(ptrYourType), pointer :: list(:)

! allocate the list of pointers to your type
allocate(list(ListCount))

! allocate each of the types pointed to the list of your type
do i=1,size(list)
allocate(list(i).pT
end do

...
! fill in fields of your type
list(iSomeEntry).pT.ID = 12345

Jim Dempsey

0 Kudos
Reply