Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

struct pointing to another struct?

0 views
Skip to first unread message

inhahe

unread,
Aug 13, 2010, 3:54:52 PM8/13/10
to
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.

Peter Otten

unread,
Aug 13, 2010, 4:07:45 PM8/13/10
to
inhahe wrote:

I think you are looking at the wrong module; you need ctypes, not struct.

http://docs.python.org/library/ctypes.html

Peter

inhahe

unread,
Aug 13, 2010, 4:28:08 PM8/13/10
to

can you (or anybody) tell me how to link one c struct to another using
ctypes? Thx

Peter Otten

unread,
Aug 13, 2010, 5:03:32 PM8/13/10
to
inhahe wrote:

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

0 new messages