tags:

views:

196

answers:

2

Hi,

I have a function in C. I want to call this function from Fortran. This function contains 2 structures passed by pointer. How do I do this in Fortran?

Ex:

struct a
{
    int x;
    float y;
};

struct b
{
    int p;
    float q;
};

In C:

fun(*a,*b);

How do I call this from Fortran? Here a is the input structure and b is the output structure. I am able to fill the structures in Fortran but they are unable to maintain any data after passing.

How do I call fun(*a,*b) in Fortran?

A: 

The answer will depend on the version of Fortran you have. In gnu for example, you can use the C_LOC function to get the address.

Mark Ransom
+1  A: 

If you are using a recent version of Fortran then there should be support for structures or records which should allow you to call the C directly. If you are using Fortran77 or earlier I would write an interface method in C that took 4 arguments, 2 to represent contents of struct a and 2 to represent struct b. The interface routine would handle the population of the input struct and getting the results from the output struct to the arguments. I would be tempted to do this even with a modern version of Fortran to avoid potential issues with memory allocation and deallocation. For example if the Fortran is returned memory allocated in the C it may not be possible for the Fortran to deallocate it.

hugok