python ctypes : get pointed type from a pointer variable

标签 python pointers types structure ctypes

我有一个包含原始字段(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/

相关文章:

java - Java 中的 Float 类型转换

string - Swift String 常量的类型是否与 String 文字不同?

python multiprocessing - 在使用 Process.start(target=func) 调用的函数中访问进程名称

Python Azure 数据工厂更新管道

Python:如何将多个参数传递给属性 getter?

c - 基于CLRS教科书的归并排序

c - 将指针传递给 scanf() 会产生段错误

javascript - Typescript:此条件将始终返回 'false',因为类型 'Type' 和 'number' 没有重叠

python - 限制某些用户添加新标签,django-taggit?

c++ - 表达式 : _CrtlsValidHeapPointer(pUserData) error