fortran access pointer returned from c

a.f90

bash 复制代码
program main
        use iso_c_binding
        implicit none
        type(c_ptr) :: ADD1
        integer R
        integer(C_INT), pointer :: S(:)  ! Declare a Fortran pointer for the array
        type(c_ptr) :: c_ptr1
        external ADD1
        integer :: i

        R = 8
        c_ptr1 = ADD1( R )

        call c_f_pointer(c_ptr1, S, [10])  ! Assume the size is 10
        print *, 'Array from C function: '
        do i = 1, 10
        print *, S(i)
        end do
end program

b.c

bash 复制代码
# include <stdlib.h>
# include <stdio.h>

int* add1_( pf )
        int *pf;
{

        printf("%d\n",*pf);
        int n = 10; // size of the array
        int *arr = (int *)malloc(n * sizeof(int)); // 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("%d ", arr[i]); // Print each element
        }
        printf("\n");
        return (int *) arr;
}

Makefile

bash 复制代码
all:
        gcc -c b.c
        gfortran a.f90 b.o -o a.out
相关推荐
船神丿男人14 分钟前
从爬楼梯到最小路径和:6 道 C++ 动态规划题的状态设计
开发语言·c++·动态规划
Evand J15 分钟前
【MATLAB例程】基于多基站AOA与距离辅助的二维运动目标定位及速度估计,附下载链接
开发语言·matlab·距离·aoa·多基站
数字供应链安全产品选型19 分钟前
从自动化扫描到 AI 自主攻防:下一代渗透测试需要突破哪些核心技术?
开发语言·php
赤生云电竞1 小时前
云电竞GPU虚拟化选型:vGPU与直通架构实践对比
java·开发语言·架构
吹什么轩1 小时前
c++高并发内存池(一):从objectpool到central cache
java·开发语言
汤姆宝库2 小时前
HNSW:分层图上的贪心检索
开发语言·python
GitCode官方2 小时前
本期精选 Rust 开源项目推荐
开发语言·rust·开源·g-star·atomgit
Pin_Qing2 小时前
当旗舰模型挂上 Flash 的价签:MiniMax M4 的定价逻辑与工程账本
服务器·开发语言·网络·人工智能·程序人生
jianqiang.xue2 小时前
STM32 STOP 模式唤醒后串口和外设全哑,问题不在唤醒源
c语言·stm32·单片机·嵌入式硬件·嵌入式·ai编程·代码审查
吴名氏.3 小时前
全栈跃迁:Java 后端 27 讲【5】多租户数据隔离
java·开发语言·springboot·全栈工程师