ctypes - pointer to array of structs?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • skip@pobox.com

    ctypes - pointer to array of structs?


    (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(Structur e):
    _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
  • Diez B. Roggisch

    #2
    Re: ctypes - pointer to array of structs?

    skip@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(Structur e):
    _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.
    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

    Comment

    • Skip Montanaro

      #3
      Re: ctypes - pointer to array of structs?

      (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.
      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



      Comment

      Working...