phlipsy
phlipsy

Reputation: 2949

How does Fortran return arrays?

The subroutine Rule_Tn in the Fortran library CUBPACK needs a parameter Integrand describing the integrated vector function. It's a

INTERFACE 
   FUNCTION Integrand(NF,X) RESULT(Value)
      USE Precision_Model
      INTEGER,                       INTENT(IN)  :: NF
      REAL(KIND=STND), DIMENSION(:), INTENT(IN)  :: X
      REAL(KIND=STND), DIMENSION(NF)             :: Value
   END FUNCTION Integrand
END INTERFACE

Since I want to call Rule_Tn from C code I need to define a function type in C exactly matching to this interface above. Thus I tried figure out how a Fortran function returns arrays. At first I supposed the following C signature

void Integrand(double* value, const int* nf, const int* x);

matches to the interface above. But far wrong! I got a segfault. And I already tested that double is the corresponding type to REAL(KIND=STND), the STND comes out of the module Precision_Model.

Now can anyone tell me what's the right signature? I'm using the GNU Fortran and C compilers.

Upvotes: 2

Views: 634

Answers (1)

Potatoswatter
Potatoswatter

Reputation: 137890

See GNU docs. It looks like you provided the arguments in a different order between Fortran and C. Try putting value last in the C prototype.

Also, you are missing bind(C) on the FUNCTION line.

Upvotes: 1

Related Questions