For a 2-dimensional array, the expression transpose(x) is not acceptable in "variable context" so you can't pass it to a routine to change the array in some way. If you want to avoid the overhead of copying the transpose into a temporary array (and later back) it would obviously be useful to have a pointer with only the array descriptor transposed (i.e. the strides and bounds swapped). Unfortunately however, there is no direct syntax for a pointer association statement with swapped indices...
Interestingly, compilers (I tested gfortran 13.3.0) seem to accept a function call that does it:
module p_hack
implicit none
contains
function palloc(a) ! pointer function, result accepted in variable context
integer, target :: a(:,:)
integer, pointer :: palloc(:,:)
palloc => a
end function
end
program test
use p_hack
integer :: A(2,2) ! NB: not even the target attribute is used!
integer, pointer :: pA(:,:)
pA => palloc( transpose(a) )
pA(1,:) = [10, 20]
pA(2,:) = [30, 40]
print *, A(1,:) ! will indeed print the tranpose of matrix A
print *, A(2,:)
end
This nicely printed the transpose:
10 30
20 40
so obviously the pointer array descriptor does access the array in transposed order, and the compiler did not create a temporary array. But I do not see how this could be guaranteed to work in all circumstances. Maybe the target attribute to the original array would help, but it seems more likely that it is just not standard-conforming to begin with. The question is:
Can something like this to get the transposed pointer be done in a standard-conforming way?
NB: a similar question could be asked for downward indexing, because this fails:
pA(1:2, 2:1,-1) => A ! compiler flags an error
even though some similar things like shifted indexing are allowed:
pA(1:2, 4:5) => A ! standard conforming, and compiler accepts it
pA(3:4, 2:3) => A ! standard conforming, and compiler accepts it
Also interesting to know would be if Fortran standardization is already looking at extending the possibilities for the use of array pointers.
Related: Returning a Pointer to a Multidimensional Array is about C, not Fortran...