ctypes - pointer to array of structs?

Discussion in 'Python' started by skip@pobox.com, Jan 3, 2008.

  1. Guest

    (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, 2008
    #1
    1. Advertising

  2. 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
     
    Diez B. Roggisch, Jan 3, 2008
    #2
    1. Advertising

  3. > > (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
     
    Skip Montanaro, Jan 4, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patricia  Van Hise

    structs with fields that are structs

    Patricia Van Hise, Apr 5, 2004, in forum: C Programming
    Replies:
    5
    Views:
    661
    Al Bowers
    Apr 5, 2004
  2. Paminu
    Replies:
    5
    Views:
    655
    Eric Sosman
    Oct 11, 2005
  3. Thomas Heller

    Re: ctypes - pointer to array of structs?

    Thomas Heller, Jan 4, 2008, in forum: Python
    Replies:
    0
    Views:
    535
    Thomas Heller
    Jan 4, 2008
  4. Replies:
    0
    Views:
    524
  5. Tuan  Bui
    Replies:
    14
    Views:
    507
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page