python ctypes 指针_Python Ctypes传递.h文件中定义的结构指针。

我认为您可能缺少的是确切地知道您希望分配结构内存的位置。下面的c代码提供了一个为struct分配内存并返回指向它的指针的函数(new_struct())。#include

#include

#include

typedef struct {

int a;

int b;

} my_struct;

my_struct *new_struct()

{

my_struct *struct_instance = (my_struct *)malloc(sizeof(my_struct));

memset(struct_instance, 0, sizeof(my_struct));

return struct_instance;

}

int modify_struct(my_struct *ms) {

ms->a = 1;

ms->b = 2;

return 0;

}

void print_struct_c(my_struct *ms) {

printf("my_struct {\n"

" a = %d\n"

" b = %d\n"

"}\n", ms->a, ms->b);

}

从Python获取指针,调用执行分配的C函数,然后可以将其传递给将其作为参数的其他C函数。import ctypes

lib_file_path = <<< path to lib file >>>

# Very simple example of how to d

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值