[英]How to initialize and pass self referential struct using ctypes in python?
I have the following structure in C: 我在C中有以下结构:
struct Entry_t{
char *key;
int data_type;
int nbitems;
size_t offset;
int size;
Entry_t *pcfg;
};
and the following function in C:(i eventually want to initialize the struct and pass it from python to C) 和C中的以下函数:(我最终想要初始化结构并将其从python传递给C)
int get_config(void *ctx, Entry_t* pconfig, void *pdest, char *key);
I have defined the same struct in python as follows: 我在python中定义了相同的结构如下:
class Entry_t(Structure): pass
Entry_t._fields_ = [
('key', c_char_p),
('data_type', c_int),
('nbitems', c_int),
('offset', c_size_t),
('size', c_int),
('pcfg', POINTER(Entry_t)),
]
testctx = Entry_t() //initialize with empty values
I have created Entry_t instance as follows: 我创建了Entry_t实例,如下所示:
type = Entry_t("e",3,1,5,12,POINTER(testctx))
i get this error when creating an instance, 我在创建实例时遇到此错误,
TypeError: expected LP_Entry instance, got CArgObject
How do i properly initialize my entry_t struct? 如何正确初始化我的entry_t结构?
type = Entry_t("e",3,1,5,12,POINTER(testctx))
When you pass testctx
to your object, use the pointer()
function (lowercase) as the uppercase variant is used to describe structs rather than construct objects. 将testctx
传递给对象时,请使用pointer()
函数(小写),因为大写变量用于描述结构而不是构造对象。 Also, it is recommended not to shadow builtin Python functions, so consider renaming your variable from "type" to something else. 此外,建议不要隐藏内置的Python函数,因此请考虑将变量从“类型”重命名为其他内容。
t1 = Entry_t("e", 3, 1, 5, 12, pointer(testctx))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.