consider the following program which uses a derived data type that has a pointer component top(i)%cel(:,:)
and tries to fill it with elements of an array arr
:
program main
implicit none
integer, parameter :: ns=6, n1=2
integer:: arr(0:ns-1), clen(n1)
type comp
integer :: nc
integer, pointer :: cel(:,:)
end type comp
type(comp), allocatable :: top(:)
integer ::i,j,k,l
open(3, file='checks.dat')
arr=[5,4,3,2,1,0]
clen=[3,3]
allocate(top(1))
top(1)%nc=2
!************ fill the pointer component of top(1) using arr elements
i=1; j=0
do k=1,top(i)%nc
allocate(top(i)%cel(k,clen(k))); top(i)%cel(k,:)=0
do l=1,clen(k)
top(i)%cel(k,l)=arr(j)
write(3,'(4(A,I0))') 'top(',i,')%cel(',k,',',l,')= ', top(i)%cel(k,l)
j=j+1
end do
end do
write(3,*)
write(3,'(A,I0,A)') '====== checking the group element ,top(',i,')%cel:'
do k=1,top(i)%nc
do l=1,size(top(i)%cel,dim=2)
write(3,*) top(i)%cel(k,l)
end do
end do
end program main
The contents of the checks.dat
file are:
top(1)%cel(1,1)= 5
top(1)%cel(1,2)= 4
top(1)%cel(1,3)= 3
top(1)%cel(2,1)= 2
top(1)%cel(2,2)= 1
top(1)%cel(2,3)= 0
====== checking the group element ,top(1)%cel:
0
0
0
2
1
0
The first write
shows me that top(i)%cel(1,3)=[5, 4, 3]
and top(i)%cel(2,3)=[2, 1, 0]
, as I wanted. However when the loop ends and I check the elements I see that top(i)%cel(1,3)=[0, 0, 0]
What am I doing wrong?