alloc complex data in c, and access in fortran

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
相关推荐
房开民7 分钟前
可变参数模板
java·开发语言·算法
t***54426 分钟前
如何在现代C++中更有效地应用这些模式
java·开发语言·c++
不知名的忻31 分钟前
Morris遍历(力扣第99题)
java·算法·leetcode·morris遍历
状元岐1 小时前
C#反射从入门到精通
java·javascript·算法
itman3011 小时前
C语言、C++与C#深度研究:从底层到现代开发演进全解析
c语言·c++·c·内存管理·编译模型
_深海凉_1 小时前
LeetCode热题100-除了自身以外数组的乘积
数据结构·算法·leetcode
Victoria.a2 小时前
python基础语法
开发语言·python
Kk.08022 小时前
项目《基于Linux下的mybash命令解释器》(一)
前端·javascript·算法
爱编码的小八嘎2 小时前
C语言完美演绎8-2
c语言
SteveSenna2 小时前
Trossen Arm MuJoCo自定义1:改变目标物体
人工智能·学习·算法·机器人