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
相关推荐
wangjialelele1 分钟前
端口号、常见协议和套接字
linux·运维·服务器·c语言·网络
whm27776 分钟前
Visual Basic 参数传送-形参与实参
开发语言·visual studio
deng-c-f7 分钟前
Linux C/C++ 学习日记(26):KCP协议(二):kcp源码分享
c语言·c++·学习·网络编程·kcp
共享家952718 分钟前
QT-常用控件(多元素控件)
开发语言·前端·qt
幸运小圣18 分钟前
Iterator迭代器 【ES6】
开发语言·javascript·es6
葱头的故事20 分钟前
将传给后端的数据转换为以formData的类型传递
开发语言·前端·javascript
雾岛听蓝24 分钟前
深入解析内存中的整数与浮点数存储
c语言·经验分享·笔记·visualstudio
Yupureki34 分钟前
从零开始的C++学习生活 9:stack_queue的入门使用和模板进阶
c语言·数据结构·c++·学习·visual studio
远远远远子35 分钟前
C++-- 内存管理
c++·算法
sprintzer1 小时前
10.6-10.15力扣模拟刷题
算法·leetcode·职场和发展