2017-12-18 329 views
1

我有一個包含原始字段(int,uint8,...)和指針的結構。 這些指針通常指向一個不同結構類型的數組,以保持深度嵌套結構。 例如,在C:python ctypes:從指針變量中獲取指針類型

struct A 
{ 
int field1; 
int field2; 
struct B *fields3; 
unsigned int countofb; 
} 

struct B 
{ 
    int anotherfield1; 
    int anotherfield2; 
} 

在蟒與ctypes的創建的結構的一個包裝和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 

感謝

回答

1

確定。 我已經憑經驗找到了答案。 只需使用屬性_type_的變量或指針類型

a=A() 
print a.fields3._type_ # struct_B 
tipo=type(a.fields3) # tipo=LP_struct_B 
print tipo._type_ # struct_B