Correct way to ccall a Fortran subroutine requiring pointers

I think the answer to your general question is this one: Calling Fortran variables from Julia - #3 by anowacki

Note that this is not specific of julia, but of the interface. Since you pass a reference, one side does not know about how the indexing was at the other side:

program main
    integer :: i, a(3), b
    do i = 1,3
        a(i) = i
    end do
    print *, a
    call test(a(2:3),b)
    print *, b
end

subroutine test(a,b)
    integer :: a(2), b
    b = a(1)
end

Will print:

           1           2           3
           2

which means that b assumed the value of what was a(2) in the main program.

You can, in Julia, use OffsetArrays to use arbitrary indexing (but then you need one additional layer of care in passing the array to fortran, since an OffsetArray is an array contained in a struct that carries the information about the offsets.

1 Like