[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

ctypes - pointer to array of structs?

skip

1/3/2008 11:11:00 PM


(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
2 Answers

Diez B. Roggisch

1/3/2008 11:21:00 PM

0

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

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

skip

1/4/2008 1:09:00 AM

0

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