ctypes - pointer to array of structs?

Diez B. Roggisch deets at nospam.web.de
Fri Jan 4 00:21:27 CET 2008

skip at 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)]
>     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)


More information about the Python-list mailing list