F77/C integration : strings

From: munehiro (munehiro_at_moc.emani.remove.and.reverse.it)
Date: 12/29/03


Date: Mon, 29 Dec 2003 16:15:29 +0100

Good morning to all,
sorry if this is maybe a faq, but i searched both newsgroups and web,
and wasn't able to find what i need.

i have this problem: a fortran code should call a C function (a
subroutine in f77 view, so returns void) that malloc space for a string
and returns it as a parameter. In C i'll do

#include <malloc.h>
#include <stdio.h>

void getString(char **ptr);

int main() {

     char **ptr;
     getString(ptr);
     printf("%s",*ptr);
}

void getString(char **ptr) {

     *ptr = malloc(10);

     strncpy(*ptr,"hello",9);

}

Apart from name mangling issues, how could i call the getString
subroutine from Fortran 77, since afaik there's no concept for pointers?
i know issues for strings usage in fortran (struct that packs pointer
and length, no \0 termination and so on) but this is not the case.

All this need is to overcome the fixed lenght charachter*n problem. I
need to read from a file some arbitrary length strings, so i must rely
on dynamical string allocation (via C, mandatory) and return these
strings to fortran.

many thanks for your attention