Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Fortran Programming Language

returning a string from a C function


Hi,

Is it possible to return a string from C?
I'm trying the below to no avail

C Fortran
         CHARACTER(len=30) fstr
         INTERFACE
          FUNCTION foo()
          CHARACTER(30) :: foo
          END FUNCTION
         END INTERFACE

         fstr=foo ()

         print 10, fstr
10       format('string returned = [',a30,']')

and the C function

char * foo_() {
   char *retstr;
   retstr=malloc(30);
   strcpy(retstr,"String returned from C");
   return retstr;

}

any suggestions?

Thanks

igl@gmail.com wrote:
> Is it possible to return a string from C?
> I'm trying the below to no avail
> C Fortran
>          CHARACTER(len=30) fstr
>          INTERFACE
>           FUNCTION foo()
>           CHARACTER(30) :: foo
>           END FUNCTION
>          END INTERFACE
>          fstr=foo ()
>          print 10, fstr
> 10       format('string returned = [',a30,']')

It isn't easy to do in Fortran.  Note that your C
function doesn't return a string, in the Fortran
sense, but a pointer.

In most cases it is fairly obvious how to write a
Fortran callable C routine, but this isn't one of
those cases.  I believe Fortran functions returning
a known (at compile time) length add it as an extra
argument before or after the other arguments, but
you should find out specifically for your implementation.

Much easier to write it as a subroutine ( (void) function)
with the string as an argument.  Otherwise, wait until
Fortran 2003 compilers are available.

> char * foo_() {
>    char *retstr;
>    retstr=malloc(30);
>    strcpy(retstr,"String returned from C");
>    return retstr;
> }

If you do this, how do you expect to free() the string?
Note that almost no C code does this because of the
need to free() the return string.  Some C library routines
return a pointer to a static string.

-- glen

In John Reid's doccument about the new features of fortran 2003, he
states tha when interacting between C and Fortran, the "len" property
of a character must always be 1.

CHARACTER(LEN=1), DIMENSION(30) :: fstr

might work better.

As far as C is concerned a string of characters is just an array of
individual char elements.  Fortran's implimentation is slightly
different since it has the len property.
 I recomend googlng John Reid's text for more information.

Hello,

    in the corrigenda to Mike Metcalf's et al., Fortran 95/2003, this
    has been changed to be allowed. So you can indeed associate an actual

character(len=10,kind=c_char) stuff

    with a char *; the Fortran-provided interface then requires

character(kind=c_char), dimension(*) :: dummy_stuff

Regards

aerospace1@hotmail.com schrieb:

On May 16, 4:39 pm, igl@gmail.com wrote:

one trick I have seen to work is to make the C string a structure -
with two components - (a pointer to the beginning of )the string and
the length.  This imitates Fortran's fixed length strings and you
should be able to pass strings back and forth without problems.
Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc