struct pointing to another struct?
Peter Otten
__peter__ at web.de
Fri Aug 13 17:03:32 EDT 2010
inhahe wrote:
> On Aug 13, 4:07 pm, Peter Otten <__pete... at web.de> wrote:
>> inhahe wrote:
>> > say i have this definition:
>>
>> > 1 typedef struct SDL_Surface {
>> > 2 Uint32 flags; /* Read-only */
>> > 3 SDL_PixelFormat *format; /* Read-only */
>> > 4 int w, h; /* Read-only */
>> > 5 Uint16 pitch; /* Read-only */
>> > 6 void *pixels; /* Read-write */
>> > 7 SDL_Rect clip_rect; /* Read-only */
>> > 8 int refcount; /* Read-mostly */
>> > 9
>> > 10 /* This structure also contains private fields not shown here
>> > */
>> > 11 } SDL_Surface;
>>
>> > notice two pointers, format and pixels.
>> > say format has this definition.
>>
>> > 1 typedef struct {
>> > 2 SDL_Palette *palette;
>> > 3 Uint8 BitsPerPixel;
>> > 4 Uint8 BytesPerPixel;
>> > 5 Uint8 Rloss, Gloss, Bloss, Aloss;
>> > 6 Uint8 Rshift, Gshift, Bshift, Ashift;
>> > 7 Uint32 Rmask, Gmask, Bmask, Amask;
>> > 8 Uint32 colorkey;
>> > 9 Uint8 alpha;
>> > 10 } SDL_PixelFormat;
>>
>> > so say i want to create a mock sdl handle and pass it to some library
>> > function that requires an sdl handle. (would it even work? would i
>> > need to use an SDL_SWSURFACE?)
>>
>> > so i make the pixelformat data using struct.pack, and make the surface
>> > data using struct.pack, but how do i link the surface data to the
>> > pixelformat data? if i just use id() it'll give me the memory address
>> > of the "box" for the string and not the string data itself. thanks.
>>
>> I think you are looking at the wrong module; you need ctypes, not struct.
>>
>> http://docs.python.org/library/ctypes.html
>>
>> Peter
>
> can you (or anybody) tell me how to link one c struct to another using
> ctypes? Thx
I know it sounds old-fashioned, but the documentation is worth reading. It
contains examples for structs, pointers and arrays, e. g.
"""
>>> from ctypes import *
>>> class cell(Structure):
... pass
...
>>> cell._fields_ = [("name", c_char_p),
... ("next", POINTER(cell))]
>>>
"""
Peter
More information about the Python-list
mailing list