SUBROUTINE c_f_string(c_str, str)
USE, INTRINSIC :: iso_c_binding, ONLY: c_ptr, c_f_pointer, c_char
TYPE(c_ptr), INTENT(in) :: c_str
CHARACTER(kind=c_char), POINTER :: arr(:)
CHARACTER(:, kind=c_char), ALLOCATABLE, INTENT(out) :: str
INTEGER(8) :: n, i
INTERFACE
! steal std c library function rather than writing our own.
FUNCTION strlen(s) bind(c, name='strlen')
USE, INTRINSIC :: iso_c_binding, ONLY: c_ptr, c_size_t
IMPLICIT NONE
!----
TYPE(c_ptr), INTENT(in), value :: s
INTEGER(c_size_t) :: strlen
END FUNCTION strlen
END INTERFACE
n = strlen(c_str)
!****
CALL c_f_pointer(c_str, arr, [n])
ALLOCATE (CHARACTER(len=n) :: str)
DO i = 1, n
str(i:i) = arr(i)
END DO
END SUBROUTINE c_f_string