Fortran structures and pointers

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

Example:

struct a
{
    int x;
    float y;
};

struct b
{
    int p;
    float q;
};

      

In C:

fun(*a,*b);

      

What should I call this from Fortran? Here a

is the input structure and b

is the output structure. I can populate structs in Fortran, but they cannot maintain any data after passing.

How do I call fun(*a,*b)

Fortran?

+2


source to share


2 answers


If you are using the latest version of Fortran then there should be support for structures or records that should allow you to call C directly. If you are using Fortran77 or earlier I would write an interface method in C that took 4 arguments, 2 to represent the contents of the struct a, and 2 to represent struct b. The interface procedure would process the collection of the input structure and receive results from the output structure to the arguments. I would be tempted to do this even with modern Fortran to avoid potential memory allocation and deallocation problems. For example, if Fortran will reclaim memory allocated in C, it may not be possible for Fortran to free it.



+1


source


The answer will depend on the Fortran version. For example, in gnu, you can use the C_LOC function to get the address.



0


source







All Articles