Problem calling a C function from Fortran

Gil Kloepfer Jr. gil at limbic.UUCP
Mon Aug 22 13:47:30 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.
|>
 [Description deleted]
|>Thanks,
|>Nate Okinaka (206)773-2687
|>
|>The following is the error message I get when I run the program:
|>-----------------------------------------------------------------------------
|>%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

[Programs deleted]

Nate,

The fact that your program aborted in DBL shows that DBL *is* actually
called (and it is, from looking at your programs).  The problem is in the
way you pass the integer*4 number to the C program.  Here is a little code
segment to show how to properly pass integer variables between fortran and
C on VMS:

Fortran code:

	PROGRAM TEST

	INTEGER*4    FVAR

	CALL CROUTINE(FVAR)

	CALL EXIT
	END

C code:

	croutine(fvar)
	long *fvar;  /* note: fortran passes by reference */
	{
		long cvar;

		cvar = *fvar + 10;  /* correct referencing of fvar */
		f77routine(&cvar);  /* must pass cvar to f77 by reference */
				    /* which is a C pointer */
	}

Fortran code:

	SUBROUTINE F77ROUTINE(CVAR)
	INTEGER*4  CVAR

	WRITE(6,*) 'CVAR = ',CVAR

	RETURN
	END

Your passing of the function with %LOC was perfect, but the way you passed
your integer variables seemed a bit obscure.  I have been using C and
FORTRAN this way with success, so this should help.

Hope it did help...

+------------------------------------+----------------------------------------+
| Gil Kloepfer, Jr.                  | Net-Address:                           |
| ICUS Software Systems              | {boulder,talcott}!icus!limbic!gil      |
| P.O. Box 1                         | Voice-net: (516) 968-6860              |
| Islip Terrace, New York  11752     | Othernet: gil at limbic.UUCP              |
+------------------------------------+----------------------------------------+



More information about the Comp.lang.c mailing list