Indeed, that works! Great trick.
Edit: in case someone wants to use this, here is the full version:
Edit 2: the default setting is supposed to be trim = .true.
.
function f_c_string(string,trim)
use, intrinsic :: iso_c_binding, only: c_char,c_null_char
implicit none
character(len=*), intent(in) :: string
logical, intent(in), optional :: trim
character(kind=c_char,len=:), allocatable :: f_c_string
logical :: trim_
trim_ = .true.
if (present(trim)) trim_ = trim
block
intrinsic trim
if (trim_) then
f_c_string = trim(string)//c_null_char
else
f_c_string = string//c_null_char
end if
end block
end function