I want to reference a 2-D array, the size of which is determined at run time, as a 1-D array without copying or mutating the original array. Since Fortran uses pointers to arrays rather than pointer arrays, direct use of a pointer doesn't work in any of the permutations I have tried. EQUIVALENCE
only seems to work on arrays of constant size, and TRANSFER
returns a copy. The specific ordering of the 1-D array is unimportant (i.e. [x11,x12,x13...]
is as good as [x11,x21,x31...]
), but when I mutate the 2-D array I would like to see the changes reflected in the 1-D array and vice versa.
Ideally I could do something like:
program arr_as_vec
implicit none
real, allocatable, target :: arr(:,:)
real, pointer :: vec(:)
integer :: dim1, dim2 ! would really be determined at runtime
dim1 = 3; dim2 = 5
allocate(arr(dim1,dim2))
call something_like_equivalence(arr, vec)
arr(1,1) = 1
arr(dim1,dim2) = 2
print *, vec(1) ! should give 1
print *, vec(dim1*dim2) ! should give 2
end program arr_as_vec
Is this possible?