struct pointing to another struct?

Peter Otten __peter__ at web.de
Fri Aug 13 16:07:45 EDT 2010


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



More information about the Python-list mailing list