!------------------------------------------------------------------------------
!
!  Program name:
!
!    array_of_pointers
!
!  Purpose:
!
!    This program shows how an array of pointers can be simulated
!    by means of a derived type having a pointer component of the 
!    desired type, and then creating an array of that derived type.
!
!------------------------------------------------------------------------------

PROGRAM main

  ! Suppose an array of pointers to reals is required. 
  ! A derived type real_pointer can be defined, whose only 
  ! component is a pointer to reals
  TYPE real_pointer
    REAL, DIMENSION(:), POINTER :: p
  END TYPE real_pointer

  ! Then an array of variables of this type can be defined
  INTEGER, PARAMETER :: n=10
  TYPE(real_pointer), DIMENSION(n) :: a
  INTEGER :: i, j

  ! The following shows each row of a lower-triangular matrix
  ! may be represented by a dynamic array of increasing size.
  ! The ith pointer can be referred to by a(i)%p.
  DO i = 1, n				 
    ALLOCATE (a(i)%p(i)) 		! allocate space for each row  
    a(i)%p = (/ (j, j = 1, i) /)	! construct array for each row
    WRITE(6, "(10F5.1)") a(i)%p 	! print values of each row
  END DO

END PROGRAM 
