I am having trouble passing a two dimensional array from Fortran to C. The following is my C function which just displays the array elements on the screen.
#include <stdio.h>
void print2(double *arr , int *n)
{
int y = *n;
printf("\n y = %d", y);
for(int i =0; i<y; i++)
{
for (int j = 0; j < y; j++)
printf("%.6g", *((arr + i*y) + j));
printf("\n");
}
}
My Fortran code so far is the following:
program linkFwithC
use, intrinsic :: iso_c_binding
implicit none
real, dimension(3,3)::a
a(1,1)=1
a(1,2)=2
a(1,3)=3
a(2,1)=4
a(2,2)=5
a(2,3)=6
a(3,1)=7
a(3,2)=8
a(3,3)=9
interface
subroutine print2(a,n) bind( c )
use, intrinsic :: iso_c_binding
type(c_ptr)::a
integer(C_INT)::n
end subroutine print2
end interface
call print2(c_loc(a),3)
end program linkFwithC
The way I am linking both files is by creating a static library for the C function and build the .lib file. Once the .lib file is built I add it to the fortran project and run the fortran project. The code runs with no errors and the n value is displayed correctly; However,the array values displayed are all wrong.
Please help !
Thanks, Anas
After further research I was able to find a work around this as follow:
The following is my C function:
The following is my Fortran code: