deepblue
deepblue

Reputation: 8596

getting struct elements in Cython

Amazingly I can't seem to find a single example of getting elements of a struct, by name (both on the web and in the cython examples).

So I'm receiving a pointer to a struct out of a C function, and would want to access those elements one by one and repackage them into a python list/dict.

maybe:

structPointer['propertyName']

or

structPointer.propertyName  

I want to get the effect of structName->propertyName.

Upvotes: 5

Views: 2936

Answers (1)

sth
sth

Reputation: 229784

Your second syntax is the correct one, but you have to have an extern declaration for the struct type:

cdef extern from "someheader.h":
   struct properties_t:
      int value1
      int value2
   properties_t* getthem()

cdef void foo():
   cdef properties_t* prop
   prop = getthem()
   i = prop.value1

Upvotes: 7

Related Questions