My c code
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
void format_time(char * argc, char ** output, int *n){
time_t rawtime;
struct tm * timeinfo;
char out[100];
time ( &rawtime );
timeinfo = localtime ( &rawtime );
strftime(out, 100, argc, timeinfo);
*output = out;
printf("%s\n",*output);
*n = strlen(*output);
}
My Fortran code
program name
use iso_c_binding
implicit none
interface
subroutine test(inp, out, n) bind(c,name= 'format_time')
use iso_c_binding
character(kind=c_char),intent(in) :: inp(*)
integer(kind=c_int),intent(out) :: n
type(c_ptr) :: out
end
end interface
character(kind=c_char, len=50), pointer :: timestr
type(c_ptr) :: tt
integer(c_int) :: nn
call test("%B"//c_null_char, tt, nn)
call c_f_pointer(tt,timestr)
print *, timestr !<-- prints junk
print*, nn
end program name
The input string properly gets passed to the c function, as evident from the print inside the c function, but the output doesn't work. How to fix this?