我有一个包含原始字段(int,uint8,...)和指针的结构。 这些指针通常指向不同结构类型的数组,以保持深度嵌套的结构。 例如在 C:
struct A
{
int field1;
int field2;
struct B *fields3;
unsigned int countofb;
}
struct B
{
int anotherfield1;
int anotherfield2;
}
在带有 ctypes 的 python 中,我创建了结构 A 和 B 的包装器。
迭代结构 A 的 _fields_
时,我到达了第三个字段 field3
,并得到了 LP_struct_B
类型的 ctype 变量。
问题是,ctypes 有没有一种方法、一个函数、一个方法可以将指针转换为指向的类型?
我需要类似的东西
a=A()
st=pointedtype(a.field3) # or also st = pointedtype2(LP_struct_B)
#desired output: st = struct_B
谢谢
最佳答案
好的。
我凭经验找到了答案。
只需使用变量或指针类型的属性_type_
a=A()
print a.fields3._type_ # struct_B
tipo=type(a.fields3) # tipo=LP_struct_B
print tipo._type_ # struct_B
关于python ctypes : get pointed type from a pointer variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47866482/