Dear,
I want to send a value to a subroutine in c. In subroutine C the value needs to be changed and that change should be kept in fortran. But not work. What do i do?
I greatly appreciate the help!
See example:
---------MAIN FORTRAN---------
program f90callC
use, intrinsic :: iso_c_binding
use interop
implicit none
byte :: i
integer(c_int) :: n
integer(c_int), allocatable, dimension(:) :: vetor
real(c_double) :: val
real(c_float), allocatable, dimension(:) :: array
n=3; val=3.14
allocate(vetor(n))
allocate(array(n))
write(*,*)' before:',val
write(*,*)' ----------------------'
write(*,*)' vector array '
do i=1,n
vetor(i)=2*i-1
array(i)=i**sqrt(2.)-1
write(*,*) vetor(i),array(i)
enddo
write(*,*)' ----------------------'
!void C
call f_function(n,vetor,val,array)
write(*,*)' after:',val
write(*,*)' ----------------------'
write(*,*)' vetor array '
do i=1,n
write(*,*) vetor(i),array(i)
enddo
write(*,*)' ----------------------'
deallocate(vetor)
deallocate(array)
end program f90callC
---------MAIN FORTRAN---------
---------MODULE FORTRAN---------
module interop
interface inter_c_function
subroutine f_function(n, vetor, v, array) bind(c, name='c_function')
use, intrinsic :: iso_c_binding
implicit none
integer(c_int), value :: n
integer(c_int), dimension(n),intent(inout) :: vetor
real(c_double),intent(inout) :: v
real(c_float), dimension(n),intent(inout) :: array
end subroutine
end interface
end module
---------MODULE FORTRAN---------
---------Subroutine in C---------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
/* Prototype */
//int fplusint(int k, int m);
float fmultfloat(float k, float m);
//extern void c_function(int n, int *vetor,double v, float *array);
void c_function(int n, int *vetor,double v, float *array) {
int i;
float aux;
//comandos
v+=(double)2.0;
printf( " Inside C: %f \n",v);
for (i = 0; i < n; ++i)
{
//vetor[i]+=n;
vetor[i]=fplusint(n,vetor[i]);
aux=(float)v;
array[i]=fmultfloat(aux,array[i]);
//array[i]*=(float)v;
printf( " %d %f \n",vetor[i],array[i]);
}
}
int fplusint(int k, int m)
{
m+=k;
return m;
}
float fmultfloat(float k, float m)
{
m*=k;
return m;
}
---------Subroutine in C---------