Extan
Extan

Reputation: 29

Pass a C pointer inside a Fortran77 subroutine

The software I am currently working on uses both languages, C and Fortran77. The .f subroutines does the mathematical part, while the C routines manages the general behavior of the software.

My problem is the following. Let's say I have one C routine routine.c, and two Fortran subroutines sub1.f and sub2.f .

routine.c calls sub1.f which than calls sub2.f :

 ... ->  routine.c  ->  sub1.f  ->  sub2.f

My header file (Header.h) is define as follows

void routine(ITG *int1, ITG *int2, char *char)  
void FORTRAN(sub1(ITG *int1, ITG *int2, char *char))  
void FORTRAN(sub2(ITG *int1, ITG *int2, char *char))

(all the vairables are arrays)

Now here is the thing : if I write in sub1.f

write(*,*) 'int1(1) =', int1(1)

I actually get the first value of the array (corresponding to int1(0) in C) But the same command line in sub2.f does not give me anything back, and I don't understand why.

Upvotes: 2

Views: 186

Answers (1)

Jeff Hammond
Jeff Hammond

Reputation: 5642

Please post all of your code so that it can be tested. I have no idea what type ITG is or what Fortran integer size you are compiling for. It's effectively impossible to debug your issue without more code of the code.

Passing character types between C and Fortran 77 is nontrivial. Because Fortran does not use null-terminated strings, there is a hidden argument for the string length. Your linker doesn't notice and it probably isn't the issue here, but it is worth noting.

I strongly recommend you use Fortran 2003 and ISO_C_BINDING for C-Fortran interoperability. It was designed for exactly this purpose and works incredibly well relative to the pile of non-portable voodoo required otherwise.

Upvotes: 1

Related Questions