I have a small package that interfaces with a C library. One of the tasks is to convert a string managed by the C library to a string that can be used in Fortran. Currently I use code like this:
character(:), allocatable, intent(out) :: value
integer :: i
type(c_ptr) :: cvalue
character(len=1), pointer, dimension(:) :: cchar
call c_f_pointer( cvalue, cchar, [strlen(cvalue)] )
allocate( character(len=size(cchar)) :: value )
do i = 1,size(cchar)
value(i:i) = cchar(i)
enddo
But I wonder if there is a more direct way to achieve this. The other way around only requires adding a NUL character.