To: J3 J3/18-258 From: Dan Nagle Subject: cstring-fstring Date: 2018-October-01 I Introduction When interoperating between C and Fortran, strings may need to be passed between procedures written in C and those written in Fortran. This raises the issue of how to convert strings between the representation used by either language. Fortran strings have a length, which is stored as metadata. C strings have a starting address and may be terminated by a null character. (This "null-terminated string" is not mandated by the C standard, but is a convention employed by several library procedures of C.) II Use-cases Fortran to C The useful length of the string must be determined, space for one more character must be found, and a null character added just beyond the useful data. Fortran to C The null character must be found, if present, and removed. I usually do this something like (I'll set from_c = ' ' before calling C) i = len_trim( from_c) if( from_c( i: i) == c_null_char ) from_c( i: i) = blank Going in either direction has extra steps. III What I have in mind Add two new intrinsic procedures, cstring() and fstring(), to provide the conversion. This raises questions about exactly how much of the conversion is performed by these procedures, and how much should be left to Fortran's existing facilities. Specifically, if every time I use cstring(), my next step is to apply c_loc(), why not build the c_loc into cstring() ? Likewise, if every time I use fstring, I just used c_f_ptr(), why not build that into fstring() ? So I'll propose some Straw Votes: What is the type of cstring()'s function result? 1. Fortran character rank-1 array 2. c_ptr 3. undecided What is the type of fstring()'s argument? 1. Fortran character rank-1 array 2. c_ptr 3. undecided The next question is whether these procedures should be plain intrinsics, or whether they should be in iso_c_bunding. So, yet another Straw Vote: cstring()/fstring() should be 1. plain instrinsics 2. intrinsic module procedures of iso_c_binding 3. undecided IV (Rough) Requirements The intrinsic function set or iso_c_binding (depends on straw vote above) has added to it the procedures cstring() and fstring(). cstring() takes a Fortran string and prepares it for use with C fstring() takes a C string and prepares it for use with Fortran The exact functionality subject to the straw votes above. Whether intrinsic or module intrinsic subject to the straw vote above.