fortrandynamic-arraysfortran-iso-c-bindingfortran2003

In Fortran2003, is 1D Assumed shape array interoperable with C?


In Fortran 2003, the allocatable array is not interoperable with C. I suppose this has something to do with additional array information stored in memory which might disturb the C interpretation.

But what if I declare a dummy argument as 1D assumed shape array? for example

subroutine outter_subroutine(ma, size_ma)
integer  :: size_ma
integer  :: ma(size_ma)

call fortran_subroutine(ma)

end subroutine

!-----------------------------

subroutine fortran_subroutine(a)
integer, intent(in) :: a(:)
integer,(kind=c_int):: ierr
...
ierr = some_c_function(a)
...
end subroutine

The interface in fortran may like

interface
function some_c_function(a)
integer(c_int) :: a(*)
end interface

while in C, the prototype maybe

int some_c_function(int *a)

Will that conform the Fortran 2003 standard?


Solution

  • In Fortran 2003, assumed shaped arrays are not interoperable.

    Assumed shaped arrays are not interoperable with C pointers to arrays in Fortran in general. They can be used when interoperating with C, but in a more complicated way using features in the standard ISO_Fortran_binding.h header from Fortran 2018, but that is much more involved than just using a C array pointer.

    You can pass an assumed shape array (or any other) to an interoperable subroutine which has an assumed size argument (a(*)). A temporary array may have to be created by the compiler to be able to do that if the array is not contiguous.