我认为您可能缺少的是确切地知道您希望分配结构内存的位置。下面的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