1

I have a structure which contains primitive fields (int, uint8, ...) and also pointers. These pointers often point to an array of different structures type in order to keep a deeply nested structure. For example in C:

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

struct B
{
  int anotherfield1;
  int anotherfield2;
}

In python with ctypes I create a wrapper of the structures A and B. iterating over _fields_ of structure A I reach the third field field3 and I get a ctype variable of type LP_struct_B .

The question is, is there a way, a function, a method of ctypes that convert a pointer to the pointed type ?

I need something like

a=A()
st=pointedtype(a.field3) # or also st = pointedtype2(LP_struct_B)
#desired output: st = struct_B

thanks

1 Answer 1

2

Ok. I've empirically found the answer. Simply use the attribute _type_ of the variable or of the pointer type

a=A()
print a.fields3._type_ # struct_B
tipo=type(a.fields3) # tipo=LP_struct_B
print tipo._type_ # struct_B
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.