Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > ctypes - pointer to array of structs?

Reply
Thread Tools

ctypes - pointer to array of structs?

 
 
skip@pobox.com
Guest
Posts: n/a
 
      01-03-2008

(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
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      01-03-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
Skip Montanaro
Guest
Posts: n/a
 
      01-04-2008
> > (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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to correctly pass “pointer-to-pointer” into DLL via ctypes? Grigory Petrov Python 4 10-27-2012 02:22 PM
pointer to an array vs pointer to pointer subramanian100in@yahoo.com, India C Programming 5 09-23-2011 10:28 AM
WindowsXP/ CTypes - How to convert ctypes array to a string? dudeja.rajat@gmail.com Python 0 08-19-2008 10:20 AM
ctypes pointer to pointer Podi Python 2 09-28-2006 04:45 PM
RE: [ctypes-users] [Ann] ctypes 0.9.0 released Henk Punt Python 0 07-23-2004 10:34 PM



Advertisments