a.f90
bash
program main
use iso_c_binding
implicit none
type(c_ptr) :: ADD1
type(c_ptr) :: c_ptr1
external ADD1
integer :: i
complex :: z
complex, pointer :: S(:)
c_ptr1 = ADD1( )
call c_f_pointer(c_ptr1, S, [10])
print *, 'Array from C function: '
do i = 1, 5
print *, S(i)
end do
! z = (1.0, 7.0) ! 0 is the real part, 5.0 is the imaginary part
! print *, 'Complex number: ', z
end program
b.c
bash
# include <stdlib.h>
# include <stdio.h>
float* add1_( )
{
int n = 10; // size of the array
float *arr = (float *)malloc(n * sizeof(float)); // allocate memory for n integers
// Check if memory allocation was successful
if (arr == NULL) {
printf("Memory allocation failed\n");
return NULL; // Return error code
}
// Initialize and print the array
for (int i = 0; i < n; i++) {
arr[i] = i * i; // Assign some values
printf("%f ", arr[i]); // Print each element
}
printf("\n");
return (float *) arr;
}
Makefile
bash
all:
gcc -c b.c -O0 -g
gfortran a.f90 b.o -O0 -g -o a.out