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
相关推荐
m0_587958955 分钟前
C++中的命令模式变体
开发语言·c++·算法
似水এ᭄往昔15 分钟前
【数据结构】--链表OJ
数据结构·算法·链表
~无忧花开~17 分钟前
React生命周期全解析
开发语言·前端·javascript·react.js·前端框架·react
剑心诀19 分钟前
02 数据结构(C) | 线性表——顺序表的基本操作
c语言·开发语言·数据结构
人间打气筒(Ada)34 分钟前
如何基于 Go-kit 开发 Web 应用:从接口层到业务层再到数据层
开发语言·后端·golang
m0_4886333236 分钟前
Windows环境下编译运行C语言程序,合适工具与方法很关键
c语言·windows·git·开发工具·编译器
2501_9249526937 分钟前
代码生成器优化策略
开发语言·c++·算法
MORE_7742 分钟前
leecode100-划分区间-贪心算法
算法·贪心算法
清风徐来QCQ1 小时前
八股文(1)
java·开发语言
lsx2024061 小时前
网站主机技术
开发语言