Tags: array, ctypes, listbut, mailing, pointer, programming, python, structs

ctypes - pointer to array of structs?

On Programmer » Python

4,422 words with 3 Comments; publish: Thu, 01 May 2008 00:47:00 GMT; (20062.50, « »)

(Is this the right place to ask ctypes questions? There's a mailing list

but the last post to it seems to have been in November 2006.)

Using ctypes I reference a structure which contains a pointer to an array of

another structure:

class SYMBOL(Structure):

_fields_ = [("symbol", c_char_p),

("num", c_int),

("units", c_int),

("baseprice", c_int),

("active", c_int)]

SYMBOL_PTR = POINTER(SYMBOL)

class TABLE(Structure):

_fields_ = [("map", SYMBOL_PTR),

("nsymbols", c_uint),

..]

Effectively, TABLE.map is an array of TABLE.nsymbols SYMBOLS. How to I

reference elements in that array? In C I would just treat TABLE.map like an

array and index into it (for i=0; i< TABLE.nsymbols; i++) ...). This is

data returned from a C library, not something I'm building in Python to pass

into C.

Thx,

Skip

All Comments

Leave a comment...

  • 3 Comments
    • skip.python.todaysummary.com.pobox.com schrieb:

      > (Is this the right place to ask ctypes questions? There's a mailing list

      > but the last post to it seems to have been in November 2006.)

      No, it's active.

      > Using ctypes I reference a structure which contains a pointer to an array

      of

      > another structure:

      > class SYMBOL(Structure):

      > _fields_ = [("symbol", c_char_p),

      > ("num", c_int),

      > ("units", c_int),

      > ("baseprice", c_int),

      > ("active", c_int)]

      > SYMBOL_PTR = POINTER(SYMBOL)

      > class TABLE(Structure):

      > _fields_ = [("map", SYMBOL_PTR),

      > ("nsymbols", c_uint),

      > ...]

      > Effectively, TABLE.map is an array of TABLE.nsymbols SYMBOLS. How to I

      > reference elements in that array? In C I would just treat TABLE.map like

      an

      > array and index into it (for i=0; i< TABLE.nsymbols; i++) ...). This is

      > data returned from a C library, not something I'm building in Python to pa

      ss

      > into C.

      I think you should be able to create an array-type with the required

      number of entries, and cast map to that. Along these lines (untested)

      ap = POINTER(SYMBOL(table.nsymbols))

      map = cast(table.map, ap)

      Diez

      #1; Thu, 01 May 2008 00:49:00 GMT
    • > > (Is this the right place to ask ctypes questions? There's a mailing list

      > No, it's active.

      Thanks. I guess the official ASPN-based archive must be dead.

      I managed to sort of get access to the array just using indexing

      as I would in C, but I'm having some problems referencing

      elements of the SYMBOL struct. I'll keep plugging away.

      Skip

      #2; Thu, 01 May 2008 00:50:00 GMT
    • skip.python.todaysummary.com.pobox.com schrieb:

      > (Is this the right place to ask ctypes questions? There's a mailing list

      > but the last post to it seems to have been in November 2006.)

      You could use the ctypes-users mailing list:

      https://lists.sourceforge.net/lists...fo/ctypes-users

      It is also available via gmane.

      > Using ctypes I reference a structure which contains a pointer to an array

      of

      > another structure:

      > class SYMBOL(Structure):

      > _fields_ = [("symbol", c_char_p),

      > ("num", c_int),

      > ("units", c_int),

      > ("baseprice", c_int),

      > ("active", c_int)]

      > SYMBOL_PTR = POINTER(SYMBOL)

      > class TABLE(Structure):

      > _fields_ = [("map", SYMBOL_PTR),

      > ("nsymbols", c_uint),

      > ...]

      > Effectively, TABLE.map is an array of TABLE.nsymbols SYMBOLS. How to I

      > reference elements in that array? In C I would just treat TABLE.map like

      an

      > array and index into it (for i=0; i< TABLE.nsymbols; i++) ...). This is

      > data returned from a C library, not something I'm building in Python to pa

      ss

      > into C.

      Assuming you got a pointer to TABLE from a function call like this:

      somefunction.restype = POINTER(TABLE)

      ptab = somefunction(...)

      then you should be able to use this code (C has the '*' operator

      to derefence pointers, Python does not so you have to use p[0] instead

      of *p):

      table = ptab[0]

      symp = table.map

      for i in range(table.nsymbols):

      sym = symp[0]

      print sym.symbol, sym.num, sym.units, sym.baseprice

      Thomas

      #3; Thu, 01 May 2008 00:51:00 GMT