1

Given a 1D array, I want to create a pointer to the array boundaries, say the first 2 and last 2 elements of the array. I have investigated a bit about this but the only non-contiguous pointer examples I have come across are those defined as strided access.

Given the following test case

program test
    implicit none

    integer, target :: a(10)
    integer, pointer :: p(:)
    integer :: i

    do i=1,10
        a(i)=i
    end do

    p => a(:)   ! Point all
    p => a(::2) ! Point every 2 elments

    print*, p
end program test

how can I point to separated array slices of the original array using a single pointer? So to be clear, in this example when printing p I would like to get 1 2 9 10.

b-fg
  • 3,959
  • 2
  • 28
  • 44

1 Answers1

0

You can't do a completely general pointer. It is the point of arrays, that they are well structured and can be accessed in a sequenced manner. That's why they have to be strided.

In your case you still have some structure, so you could point to those pairs using a 2D array.

Here was an example.with equivalence. However equivalence cannot be used with target for an unknown reason in standard Fortran.

One could use a 2D pointer p as:

integer, pointer :: p(:,:), b(:,:)

b(1:2,1:5) => a

p => b(:,1::4)

but, again, 2D pointer p is required here as far as I can see.

You could also use a C-style cast for the first re-map, but is is probably useless:

type(c_ptr) ptr

ptr = c_loc(a)
call c_f_pointer(ptr, b, [2,5])

p =>  b(:,1::4)

I can't find a way with a 1D pointer.