471,325 Members | 1,458 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

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(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
Jan 3 '08 #1
2 5843
sk**@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
Jan 3 '08 #2
(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

Jan 4 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Thomas Heller | last post: by
10 posts views Thread by Kieran Simkin | last post: by
15 posts views Thread by Paminu | last post: by
reply views Thread by chris.atlee | last post: by
3 posts views Thread by Chris AtLee | last post: by
14 posts views Thread by Szabolcs Borsanyi | last post: by
3 posts views Thread by Andrew Lentvorski | last post: by
2 posts views Thread by overdrigzed | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.