Problem calling a C function from Fortran

Larry Jones scjones at sdrc.UUCP
Mon Aug 22 02:35:27 AEST 1988


In article <1134 at ssc-bee.ssc-vax.UUCP>, okinaka at ssc-vax.UUCP (Nathan Okinaka) writes:
< I'd appreciate if anybody out there could help me with a problem I'm having 
< calling a function written in C from a Fortran program on the VAX/VMS.

Well, the problem is actually calling a FORTRAN subroutine from C, but read on.
 
< I'm passing two arguments to a C function called "test".  The first one is 
< an integer*4 varaible and the second one is an external function.  The first 
< argument gets passed ok but the external function "dbl" does not seem to get 
< passed ok.

Sure it does, you just called it wrong.
 
< The C function requires the address of where the function "dbl" starts so
< I'm passing the value of the address of the function "dbl" to the C function
< "test" but it does not seem to work.  I'm wondering what Fortran does when you
< try and get %loc(dbl).  Is this really the address of where this function 
< starts?  What am I doing wrong?  Any help would be appreciatted.  This is a
< test program I'm trying out which I need to use in another bigger program.
< 
< Thanks,
< Nate Okinaka (206)773-2687
< 
< The following is the error message I get when I run the program:
< -----------------------------------------------------------------------------
< [okinaka]> r ext
< %loc(dbl) =        4848
< %loc(dbl) in hex =    12F0
< iangle =          90
< iangle in hex =      5A
< This is a test of external function
< x = 90, f = 4848 (decial)
< x = 5a, f = 12f0 (hex)
< %SYSTEM-F-ACCVIO, access violation, reason mask=00, virtual address=0000005A, 
< PC=000012F9, PSL=03C00020
< %TRACE-F-TRACEBACK, symbolic stack dump follows
< module name     routine name                     line       rel PC    abs PC
< DBL             DBL                                 5      00000009  000012F9
< TEST            test                                9      00000039  000013AD
< EXT$MAIN        EXT$MAIN                           17      000000AA  000012AA

Well, the traceback shows that your FORTRAN main program did call your C
function which then called the FORTRAN subroutine so that's not the problem.
The problem is that when the C function calls the FORTRAN subroutine, it passes
the VALUE of the argument instead of the ADDRESS.  When the FORTRAN subroutine
tried to access the variable, it used the value as an address and tried to
get the value stored there and that's what caused the access violation.
Just pass the address instead and everything will work fine.

----
Larry Jones                         UUCP: uunet!sdrc!scjones
SDRC                                      scjones at sdrc
2000 Eastman Dr.                    BIX:  ltl
Milford, OH  45150                  AT&T: (513) 576-2070
Nancy Reagan on superconductivity: "Just say mho."



More information about the Comp.lang.c mailing list