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

G

Guest

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 ?
 
D

Diez B. Roggisch

人言è½æ—¥æ˜¯å¤©æ¶¯ï¼Œæœ›æžå¤©æ¶¯ä¸è§å®¶ said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top