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
相关推荐
€8112 分钟前
Java入门级教程23——配置Nginx服务器、轻量级HTTP服务开发、前后端分离实现完整应用系统
java·开发语言·仓颉·生成验证码
yunson_Liu5 分钟前
编写Python脚本在域名过期10天内将域名信息发送到钉钉
开发语言·python·钉钉
星秀日23 分钟前
框架--SpringMVC
java·开发语言·servlet
勤奋菲菲1 小时前
Vue3+Three.js:requestAnimationFrame的详细介绍
开发语言·javascript·three.js·前端可视化
要天天开心啊1 小时前
Java序列化和反序列化
java·开发语言
二宝1522 小时前
黑马商城day1-MyBatis-Plus
java·开发语言·mybatis
Porunarufu2 小时前
JAVA·类和对象③封装及包
java·开发语言
煜3643 小时前
C++异常与智能指针
开发语言·c++
wydaicls3 小时前
AIDL 接口的定义与生成,使用
java·开发语言
云草桑3 小时前
C#入坑JAVA 使用XXLJob
java·开发语言·c#