I'm wondering if it is legal/safe to modify intent(in)
dummy arguments through pointers associated with corresponding actual arguments. Specifically when the pointer association was established before the subroutine call.
For example, is the following OK (it seems to work fine with gfortran)?
program test
implicit none
type compound
integer, allocatable :: A1(:)
integer, pointer :: A2(:,:)
end type compound
type(compound), target :: my
integer :: n=5, i
allocate(my%A1(n**2))
my%A2(1:n,1:n) => my%A1(:)
do i=1,n**2
my%A1(i) = i
end do
do i=1,n
print *, my%A2(i,:)
end do
call assign_A(my)
do i=1,n
print *, my%A2(i,:)
end do
contains
subroutine assign_A(var)
type(compound), intent(in) :: var
var%A2(:,:) = 42
end subroutine
end program test
The trick is a user-defined type that contains a pointer and a target, the former pointing to the latter. An instance of this is passed as intent(in)
and in the subroutine it is modified through the pointer component (the values pointed by intent(in)
pointers can be modified). I'm a bit surprised that this works, but maybe it's just a missing diagnostic by the compiler. If I was changing var%A1
directly that would of course fail.
EDIT: Output from the above program (compiled with gfortran 7.5.0):
1 6 11 16 21
2 7 12 17 22
3 8 13 18 23
4 9 14 19 24
5 10 15 20 25
42 42 42 42 42
42 42 42 42 42
42 42 42 42 42
42 42 42 42 42
42 42 42 42 42