Can I have a pointer array that points to an incontiguous section of a target array? In the following program:
program test
implicit none
integer :: i
integer, dimension(4), target :: orig
integer, dimension(:), pointer :: ref
orig = (/ (i , i = 1,4) /)
ref(1:2) => orig(1:2)
ref(3:3) => orig(4:4)
print *, 'orig = ', orig
print *, 'ref = ', ref
end program test
I want the pointer ref to point to the incontiguous section (/1,2,4/) of the target array orig. The code compiles without a warning, however the output is not desired:
code output:
orig = 1 2 3 4
ref = 4
It appears as the compiler forces the pointer to point to a contiguous subsection even though I first point to indices 1:2 but compiler only forces pointing to 4:4. As a result my pointer at the end only points to one entry.
I can pack the entries (/1,2,4/) into another array and then use it contiguously but this would not be efficient Fortran coding for large data set. Do you have any idea how to make this working?
Your pointer assignments have what is known as bounds remapping. The "entire" pointer object is pointer assigned, but it is given the bounds indicated in the parentheses on the left hand side. This lets you have a lower bound other than one and have the rank of the pointer differ from the target. This feature is similar to the capabilities that you have with array argument association when the dummy array is explicit or assumed size.
You cannot have a pointer point to arbitrary elements of a target array - the elements in the target have to be "regular" - have a constant stride between them.
As an alternative to repacking you can obviously store those indices in an array and use the combination of the whole target and that array of indices to get the behaviour that you want, perhaps by wrapping a pointer to the whole target and the array of indices in a derived type with defined assignments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With