Mh, this follows the same structure but seems to work ![]()
// c.c
const char *c_arr[] = {
"Hello",
};
const char *get_c_string(void) { return c_arr[0]; }
program test
use :: iso_c_binding
implicit none
character(len = :), allocatable :: str
str = get_string_f()
print *, str
contains
function get_string_f() result(str)
character(len = :), allocatable :: str
type(c_ptr) :: cstr
integer(c_size_t) :: cstr_len
interface
function get_string_c() result(ptr) bind(c, name="get_c_string")
import :: c_ptr
type(c_ptr) :: ptr
end function
function strlen_c(ptr) result(l) bind(c, name="strlen")
import :: c_ptr, c_size_t
type(c_ptr), value :: ptr
integer(c_size_t) :: l
end function
end interface
cstr = get_string_c()
cstr_len = strlen_c(cstr)
block
character(len = cstr_len, kind = c_char), pointer :: str_ptr
call c_f_pointer(cstr, str_ptr)
allocate(str, source=str_ptr)
end block
print *, '"', str, '"', len(str), allocated(str)
end function
end program