Problem calling a C function from Fortran

Nathan Okinaka okinaka at ssc-vax.UUCP
Sat Aug 20 03:54:32 AEST 1988


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);
}



More information about the Comp.lang.c mailing list