MODULE iso_c_utilities
  use iso_c_binding
  CHARACTER(C_CHAR), DIMENSION(1), SAVE, TARGET, private :: dummy_string = "?"

  INTERFACE
     FUNCTION strlen(string) RESULT(len) BIND(C,name="strlen")
       use iso_c_binding
       TYPE(C_PTR), VALUE :: string !a C pointer
     END FUNCTION strlen
  END INTERFACE
CONTAINS
  FUNCTION c_f_string(cptr) RESULT (fptr)

    TYPE(C_PTR), INTENT(IN) :: cptr
    CHARACTER(C_CHAR), DIMENSION(:), POINTER :: fptr


    IF (C_ASSOCIATED(cptr)) THEN
       CALL C_F_POINTER(FPTR=fptr, CPTR=cptr, SHAPE=[strlen(cptr)])
    ELSE
       !to avoid segmentation fault, associate fptr with a dummy target
       fptr => dummy_string
    END IF
  END FUNCTION c_f_string
END MODULE iso_c_utilities
