programming difficulty
William Labbett

hi,

I got a struct and an array of pointers to such structs :-

struct scenery_object {
    int exists;
    int w, h;
    int x, y;
    int blit_to_x, blit_to_y;
    BITMAP *thing;
};

struct scenery_object *scenery_objects[MAX_OBJECTS];

I need to be able to initialise what the pointers point to.

If I do this :-

struct scenery_object {
    int exists;
    int w, h;
    int x, y;
    int blit_to_x, blit_to_y;
    BITMAP *thing;
};

struct scenery_object *scenery_objects[MAX_OBJECTS] =
{
    {1, 20, 30, 25, 25, 0, 0, bitmap}
};

(I've only initialised one member deliberately.)

Wouldn't this be wrong ?

Is there a way to initialise the structs not the pointers ?

CGamesPlay

(In C) Nope, you can't do that.

William Labbett

That sucks really. I take it you can in C++.

CGamesPlay

In C++ you can give the struct a constructor and initializing it using new. Although that really isn't practical :)

[append]

If you are initializing them that way, why are you using pointers?

William Labbett

To be honest I just thought I aught to. I thought large structures are usually accessed with pointers to save copying them around. Is this wrong ?

CGamesPlay

No, large structures are allocated using pointers to keep them off the stack, which has limited space. You can take the address of any variable, whether ir was made with malloc, or a local, or a global, or anything. Taking the address of a variable--the same as making a pointer to it--is done like this:

struct scenery_object so;
struct scenery_object* ptr;
ptr = &so;

[append]
Oh, what you said about saving copying them around is valid, but you don't need to allocate them using malloc to save on the copying (see above).

William Labbett

Okay thanks CGamesPlay - you're a good man :) I'll see how I get on without the pointers.

Thread #587955. Printed from Allegro.cc