I have the following C and Fortran code where I would like to exchange some data
FUNCTION exchange_data(data) bind(c,name='exchange_data')
use iso_c_binding
integer(kind=c_int) :: exchange_data
real(kind=c_double), intent(inout), dimension(*) :: data
END FUNCTION exchange_data
....
WRITE(*,*), "Sent data to C"
DO I=1,NumBl
DO J=1,WindSpeedCoordNr
WRITE(*, FMT2), GLOBAL_COORD_ALONG_BEAM(I, J, :)
END DO
END DO
cflag = exchange_data(GLOBAL_COORD_ALONG_BEAM)
WRITE(*,*), "Received data from C"
DO I=1,NumBl
DO J=1,WindSpeedCoordNr
WRITE(*, FMT2), GLOBAL_COORD_ALONG_BEAM(I, J, :)
END DO
END DO
And the following test C code:
int exchange_data(double* positions)
{
printf("Received data from Fortran");
bladepositions = positions;
for (int i = 0; i < numbld; i++) {
for (int j = 0; j < datapointnr; j++) {
printf("[");
for (int k = 0; k < 3; k++) {
printf("%5.4f ", bladepositions[3 * datapointnr * k + 3 * j + i]);
windspeedalongblade[3 * datapointnr * k + 3 * j + i] = 1.0;
}
printf("]\r\n");
}
}
positions = windspeedalongblade;
printf("Data will be send from C");
for (int i = 0; i < numbld; i++) {
for (int j = 0; j < datapointnr; j++) {
printf("[");
for (int k = 0; k < 3; k++) {
printf("%5.4f ", positions[3 * datapointnr * k + 3 * j + i]);
}
printf("]\r\n");
}
}
return 0;
}
This has the following output
Sent data to C
-18.6593 -29.1175 137.0735
-18.8588 -29.1308 137.0803
-19.0582 -29.1441 137.0871
Received data from Fortran
[-18.6593 -29.1175 137.0735 ]
[-18.8588 -29.1308 137.0803 ]
[-19.0582 -29.1441 137.0871 ]
Data will be send from C
[1.0000 1.0000 1.0000 ]
[1.0000 1.0000 1.0000 ]
[1.0000 1.0000 1.0000 ]
Received data from C
-18.6593 -29.1175 137.0735
-18.8588 -29.1308 137.0803
-19.0582 -29.1441 137.0871
I seems I can transfer data to the C function but not back to Fortran code. How can I achieve that?