ctypes: how to make a structure pointer to point to a buffer

Discussion in 'Python' started by =?utf-8?B?5Lq66KiA6JC95pel5piv5aSp5rav77yM5pyb5p6B, Apr 23, 2007.

  1. first, I'm try the POINTER to convesion the pointer type. but failed.

    class STUDENT(Structure):
    _fields_ = [('name', c_int),
    ('id', c_int),
    ('addition', c_ubyte)]

    buffer = c_byte * 1024
    student_p = cast(buffer, POINTER(STUDENT))

    The parameter of the POINTER must be ctypes type.
    How could I attach the buffer pointer to the structure STUDENT ?
    =?utf-8?B?5Lq66KiA6JC95pel5piv5aSp5rav77yM5pyb5p6B, Apr 23, 2007
    #1
    1. Advertising

  2. 人言è½æ—¥æ˜¯å¤©æ¶¯ï¼Œæœ›æžå¤©æ¶¯ä¸è§å®¶ wrote:

    > first, I'm try the POINTER to convesion the pointer type. but failed.
    >
    > class STUDENT(Structure):
    > _fields_ = [('name', c_int),
    > ('id', c_int),
    > ('addition', c_ubyte)]
    >
    > buffer = c_byte * 1024
    > student_p = cast(buffer, POINTER(STUDENT))
    >
    > The parameter of the POINTER must be ctypes type.
    > How could I attach the buffer pointer to the structure STUDENT ?


    I think it should work like this:

    from ctypes import *

    class STUDENT(Structure):
    _fields_ = [('name', c_int),
    ('id', c_int),
    ('addition', c_ubyte)]

    buffer = (c_byte * 1024)()
    buffer_p = pointer(buffer)
    student_p = cast(buffer_p, POINTER(STUDENT))

    print student_p


    Diez
    Diez B. Roggisch, Apr 23, 2007
    #2
    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. Podi

    ctypes pointer to pointer

    Podi, Sep 28, 2006, in forum: Python
    Replies:
    2
    Views:
    498
  2. Grigory Petrov
    Replies:
    4
    Views:
    1,190
  3. Wanderer
    Replies:
    4
    Views:
    1,058
    Wanderer
    Mar 19, 2011
  4. Jesse R
    Replies:
    1
    Views:
    429
    Wanderer
    Jul 11, 2011
  5. Saraswati lakki
    Replies:
    0
    Views:
    1,284
    Saraswati lakki
    Jan 6, 2012
Loading...

Share This Page