I am trying to create an array of pointers in Fortran 90, as described here. Each pointer in this array is then associated with an array of floats, to be allocated at run time by dereferencing the pointer. See the below example:
program test
type ptr
double precision, pointer :: p(:)
end type ptr
integer :: i, n=5
type(ptr), dimension(3) :: ptrs
double precision, pointer :: a(:), b(:), c(:)
ptrs(1)%p => a
ptrs(2)%p => b
ptrs(3)%p => c
do i=1,3
allocate(ptrs(i)%p(n))
ptrs(i)%p = 0d0
enddo
write(6, *) ptrs(1)%p
write(6, *) a
end program test
Result:
$ gfortran -o test test.f90 && ./test
0.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000 0.0000000000000000
At line 20 of file test.f90 (unit = 6, file = 'stdout')
Internal Error: list_formatted_write(): Bad type
The error is thrown because a
is not allocated. In an ideal world, ptrs(1)%p
and a
should be identical, but apparently this world is flawed. My question: How do I correctly allocate a
, b
, and c
?