Python/Fortran interoperability

sturlamolden sturlamolden at yahoo.no
Mon Aug 24 01:59:34 CEST 2009


On 24 Aug, 00:02, Dennis Lee Bieber <wlfr... at ix.netcom.com> wrote:

>         That's a C language problem -- since a string in C is just an array
> of character. The last FORTRAN dialect (and implementation) I used
> passed strings

On 24 Aug, 00:02, Dennis Lee Bieber <wlfr... at ix.netcom.com> wrote:

> values -- FORTRAN strings were typically static, the called function
> could not reallocate to fit in longer data and update the descriptor to
> match)


It is possible to pass an arbitrary length. You have to convert a
pointer from C to Fortran:

subroutine foobar(cstr) bind(c, name='foobar')
    use, intrinsic :: iso_c_binding
    type(c_ptr) :: cstr
    character(*), pointer :: fstr
    call c_f_pointer(cptr, fptr)

If you need the length, call strlen from libc.

I think the OP has misunderstood how the Fortran 2003 C bindings work.
They don't convert C pointers to Fortran strings or Fortran arryas.
They convert C pointers to Fortran pointers.


Sturla











More information about the Python-list mailing list