OpenVMS (VAX) Fortran function returning a character*(*) to C

298 views Asked by At

A OpenVMS (VAX) FORTRAN subroutine can be passed a character*(*):

subroutine forsub (in)
character*(*) in
type *, in
return
end

from a C function:

#include<stdio.h>
#include <descrip.h>
extern void forsub();
main()
{
    auto $DESCRIPTOR(in_string, "VMS pass from c to fortran.");
    forsub(&in_string);
}

How is the OpenVMS (VAX) FORTRAN function that returns a character*(*):

character*(*) function forfunc (in)
character*(*) in
forfunc = in
return
end

handled in the C code:

#include<stdio.h>
#include <descrip.h>
extern ?????? forfunc();
main()
{
    auto $DESCRIPTOR(in_string, "VMS fortran function return to c.");
    ??????? = forfunc(&in_string);
}
  • OpenVMS V6.2
  • Digital Fortran 77 V6.5-188
  • DEC C V6.0-001
2

There are 2 answers

0
user2116290 On BEST ANSWER

Example 3-5 in this (old?) C User's Guide probably explains how to do this: you need the already mentioned hidden argument. An example would be:

#include <stdio.h>
#include <descrip.h>
extern void forfunc();
main()
{
    auto $DESCRIPTOR(in_string, "VMS fortran function return to c.");
    char buffer[64];
    struct dsc$descriptor_s out_string = {
        sizeof buffer, DSC$K_DTYPE_T, DSC$K_CLASS_S, buffer};
    forfunc (&out_string, &in_string);
    printf ("%.*s\n", out_string.dsc$w_length, out_string.dsc$a_pointer);
}
1
Fran On

From DIGITAL Visual Fortran Version V6.0A Help:

Fortran functions that return a character string using the syntax CHARACTER*(*) place a hidden string argument and the address of the string at the beginning of the argument list.

C functions that implement such a Fortran function call must declare this hidden string argument explicitly and use it to return a value. The C return type should be void. However, you are more likely to avoid errors by not using character-string return functions. Use subroutines or place the strings into modules or global variables whenever possible.