Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » programming difficulty

Credits go to CGamesPlay for helping out!
This thread is locked; no one can reply to it. rss feed Print
programming difficulty
William Labbett
Member #4,486
March 2004
avatar

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
Member #2,559
July 2002
avatar

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

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

William Labbett
Member #4,486
March 2004
avatar

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

CGamesPlay
Member #2,559
July 2002
avatar

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?

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

William Labbett
Member #4,486
March 2004
avatar

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
Member #2,559
July 2002
avatar

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).

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

William Labbett
Member #4,486
March 2004
avatar

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

Go to: