0

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?

Eular
  • 1,707
  • 4
  • 26
  • 50
  • What is the output you see? Note that you've declared the function result a scalar of length 1. – francescalus Mar 05 '21 at 13:15
  • I get a junk result (to say literally an `@`). Also `Return type of BIND(C) function 'txt' at (1) cannot be an array` – Eular Mar 05 '21 at 13:23
  • Yes, the result cannot be an array, so you would need something like in [this other question](https://stackoverflow.com/q/63412077/3157076). – francescalus Mar 05 '21 at 13:25
  • Your (C) function returns a pointer to an array, so the linked question applies. Note that you can declare `character(kind=c_char, len=50), pointer :: x` so that `call c_f_pointer(get(...), x)` behaves as you'd like. – francescalus Mar 05 '21 at 13:44
  • 1
    Ok, thanks, that solves my question – Eular Mar 05 '21 at 13:54
  • @francescalus, the above method also returns some unknown characters along with the actual time string, guess I have the terminate the string after the actual values, but I don't know how to get the actual character length, the `trim` also doesn't work. Can you help me with that? – Eular Mar 08 '21 at 10:12
  • If the C function result is null terminated, you can use (C's) `strnlen` to find its length then use just that part of the Fortran variable. Or search in Fortran for `c_null_char`. `trim` works on trailing spaces, not null char – francescalus Mar 08 '21 at 10:43
  • on the other hand, if I do the `c` function as this `void format_time(char * argc, char ** output, int *n)` to modify the output and string length in the `c` side, how do I get the proper output` as a string on the `Fortran` side? I have tried defining it as `type(c_ptr)` for `output` and converting it with `c_f_pointer` but it doesn't work. – Eular Mar 08 '21 at 17:02
  • Does `print *, timestr(:nn)` return what you expect? – francescalus Mar 09 '21 at 17:22
  • no, it shows some junk. I've updated the question so that you can see the new code. – Eular Mar 09 '21 at 18:33
  • In your C function `char out[100];` doesn't have `static` attribute, so doesn't have a lifetime beyond the function. In particular, there's nothing for the `tt` left to point at in the Fortran program. – francescalus Mar 09 '21 at 18:34
  • Oh, yes I see the problem now. Is there any way to pass the `output` variable in c to pass it directly to `strftime` function? – Eular Mar 09 '21 at 18:42
  • If what you care about is just the case of trying to populate a Fortran variable from `strftime`, then yes you can just pass a Fortran character variable through and a pointer to the start to `strftime`. You want to use `type(c_ptr)` when you have to worry about variables with their memory managed by C. – francescalus Mar 09 '21 at 18:53
  • `you can just pass a Fortran character variable through and a pointer to the start`, sry, but I didn't quite get that, did you mean instead of `c_ptr`, I can use `character(kind=c_char) :: out(*)` in fortran and for `c` `void format_time(char * argc, char * output, int *n)` and directly pass `output` to `strftime`? – Eular Mar 09 '21 at 19:13
  • Yes. (There may be some fiddling to make sure things match up, but the crucial thing is that the "pointer to first element of the character array" that goes to the C wrapper function is the same pointer that needs to go to `strftime`. That memory block is managed by Fortran and is entirely interoperable.) – francescalus Mar 09 '21 at 19:17
  • Is this okay https://gist.github.com/Koushikphy/04f8f73f96d98774e07d3fffe015449e – Eular Mar 09 '21 at 19:25

0 Answers0