Xref: utzoo comp.sys.dec:743 comp.lang.fortran:988 comp.lang.c:11948 Path: utzoo!utgpu!attcan!uunet!lll-winken!lll-tis!ames!mailrus!cornell!uw-beaver!fluke!ssc-vax!okinaka From: okinaka@ssc-vax.UUCP (Nathan Okinaka) Newsgroups: comp.sys.dec,comp.lang.fortran,comp.lang.c Subject: Problem calling a C function from Fortran Keywords: Fortran, C, external function in Fortran Message-ID: <1134@ssc-bee.ssc-vax.UUCP> Date: 19 Aug 88 17:54:32 GMT Organization: Boeing Aerospace Corp., Seattle WA Lines: 88 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. 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. 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 ----------------------------------------------------------------------------- The following lists the Fortran program and the C function that I'm calling: C------------------------------------------------------------------------ C Main Fortran Program C------------------------------------------------------------------------ implicit none external dbl integer*4 func_adr integer*4 test, temp integer*4 dbl integer*4 iangle, istat iangle = 90 func_adr = %loc(dbl) type *,'%loc(dbl) =',func_adr write(5,100)func_adr 100 format(' %loc(dbl) in hex =',z8) type *,'iangle =',iangle write(5,110)iangle 110 format(' iangle in hex =',z8) istat = test(%val(iangle), %val(func_adr)) type *,' ' type *,'istat=',istat end integer*4 function dbl(x) implicit none integer*4 x dbl = (2 * x) type *,' ' type *,'dbl> x=',x,' dbl=',dbl type *,' ' return end /*---------------------------------------------------------------------------*/ /* C Funtion test */ /*---------------------------------------------------------------------------*/ int test(x,f) int x; int (*f)(); { int y; printf("\nThis is a test of external function\n"); printf("x = %d, f = %d (decial)\n",x,f); printf("x = %x, f = %x (hex)\n",x,f); y = (*f)(x); printf("y = %d\n",y); return(y); }