ctypes的 - 通过一个结构的指针另一个结构 [英] ctypes - passing a struct with a pointer to another struct
问题描述
在我的C code我有:
In my C code I have:
typedef struct{
int info1;
int info2;
MoreData* md;
} BasicData;
typedef struct{
int extinfo[100];
char stuff[100];
} MoreData;
现在我有一个C库函数,它接受BasicData作为参数,我想从Python中调用它。
要做到这一点,我建立一个ctypes类:
Now I have a C library function which takes BasicData as argument and I want to call it from Python. To do this I construct a ctypes class:
class BasicData(Structure):
_fields_ = [("info1", c_int),
("info2", c_int),
("md", ??????)]
我的问题是我该怎么补进????空间,能够通过这个结构为C函数(或者我需要完全不同的东西?)。 MOREDATA计算过程中只是用来和我不需要从它在我的Python $ C $阅读℃,它就会从C库级别处理,以便分配内存。我只需要通过使用正确的指针类型BasicData结构。
My question is what do I fill into ???? space to be able to pass this struct to C function (or do I need something completely different?). MoreData is just used during computations and I don't need to read from it in my Python code so allocating memory for it will be handled from C library level. I just need to pass a BasicData struct with correct pointer type.
推荐答案
如果您不需要分配或使用 MOREDATA
结构,那么 MD
只是一个普通的指针就ctypes的关注。使用无效*
- 即 c_void_p
If you don't need to allocate or use the MoreData
struct, then md
is just a generic pointer as far as ctypes is concerned. Use a void *
-- i.e. c_void_p
.
这篇关于ctypes的 - 通过一个结构的指针另一个结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!