In preparing a minimal working example for a pointer-related question, I found that a pointer to multi-dimensional arrays behaved in a way I did not expect. In the program below, I have two-multidimensional arrays a
and b
, with the rank of b
being one less than that of a
. I am trying to use a pointer c
to represent the “concatenation” of a
and b
along one dimension. However, I must misunderstand something about pointer semantics, because this does not seem to be what happens.
implicit none
integer, parameter :: k = 40, l = 30, m = 20, n = 10
real, target, allocatable :: a(:,:,:,:), b(:,:,:)
real, pointer :: c(:,:,:,:)
allocate (a(k,l,m,n), b(k,m,n))
allocate (c(k,l+1,m,n))
call random_number(a)
call random_number(b)
c(1:k,1:l,1:m,1:n) => a
print *, shape(c) ! 40 30 20 10 -- expected 40 31 20 10
c(1:k,l+1:l+1,1:m,1:n) => b
print *, shape(c) ! 40 1 20 10 -- expected 40 31 20 10
end
For completeness, this program compiled and executed without warning using gfortran 9.2.0 with the options “-std=f2018 -Wall -Wextra”. Is this how pointers are meant to work? I’d appreciate if anyone can shed some light on this. I think @arjen was recently looking into something similar having to do the concatenation operator in mathematics of arrays.