Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Two questions about pointers...

SephirothSephiroth Fayetteville, NC, USAPosts: 1,035Member
OK, I have figured out the basics of using pointers, but now I have to figure out two more thigns to be able to use them properly. One is accessing a pointer in a pointer, and the other is setting the different values of a structure pointed to by a pointer. First, let me show you my structures:
[code]
typedef struct
{
bool solid, pushable;
int num_poly;
char name[256], target[256];
STATIC_VERTEX orig_vertex;
POLYGON *plist;
} OBJECT;

typedef struct
{
int num_spr;
float num_obj;
SPRITE spr_list[2048];
OBJECT *olist;
} LEVELINFO;

OBJECT brush;
LEVELINFO map;
[/code]
Now how would I properly allocate memory for LEVELINFO structures, such as the demo, map? Here is what I have toyed with:
[code]
...
brush.plist = (POLYGON *)malloc(sizeof(POLYGON));
//Above works aok!

map.olist = (OBJECT *)malloc(sizeof(OBJECT));
map.olist.plist = (POLYGON *)malloc(sizeof(POLYGON));
//The problems are how to allocate ram for the pointer in a pointer
[/code]
OK, that aside, say I wanted to set or retrieve a var in one of the many OBJECT structs. Could I (after creating, say, five objects via pointers) access them via brush.polygon[x].x? If not, how exactly would i reference them individually? Thanks in advance for the help.

-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red]

Comments

  • weiccoweicco Posts: 950Member
    : OK, I have figured out the basics of using pointers, but now I have to figure out two more thigns to be able to use them properly. One is accessing a pointer in a pointer, and the other is setting the different values of a structure pointed to by a pointer. First, let me show you my structures:
    : [code]
    : typedef struct
    : {
    : bool solid, pushable;
    : int num_poly;
    : char name[256], target[256];
    : STATIC_VERTEX orig_vertex;
    : POLYGON *plist;
    : } OBJECT;
    :
    : typedef struct
    : {
    : int num_spr;
    : float num_obj;
    : SPRITE spr_list[2048];
    : OBJECT *olist;
    : } LEVELINFO;
    :
    : OBJECT brush;
    : LEVELINFO map;
    : [/code]
    : Now how would I properly allocate memory for LEVELINFO structures, such as the demo, map? Here is what I have toyed with:
    : [code]
    : ...
    : brush.plist = (POLYGON *)malloc(sizeof(POLYGON));
    : //Above works aok!
    :
    : map.olist = (OBJECT *)malloc(sizeof(OBJECT));
    : map.olist.plist = (POLYGON *)malloc(sizeof(POLYGON));
    : //The problems are how to allocate ram for the pointer in a pointer
    : [/code]
    : OK, that aside, say I wanted to set or retrieve a var in one of the many OBJECT structs. Could I (after creating, say, five objects via pointers) access them via brush.polygon[x].x? If not, how exactly would i reference them individually? Thanks in advance for the help.
    : -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red]
    :

    I didn't quite get what you were looking for but here's something. I hope it helps.
    First allocate memory for the struct:

    OBJECT *obj = (OBJECT *)malloc(sizeof(OBJECT));

    then allocate memory for buffer inside struct:

    obj->plist = (POLYGON *)malloc(sizeof(POLYGON));



  • SephirothSephiroth Fayetteville, NC, USAPosts: 1,035Member
    OK, I can now allocate mempry to all of the objects and their objects properly, but now, how do I go back and read one I previously set? Say I use brush once and setup a polygon in it, then reallocate ram to make a second one or third one, how would I go back and read the values of the first one?


    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red]


  • weiccoweicco Posts: 950Member
    Hmmm... I'm not sure what you mean... Do you need to access pointer->pointer->value? If this the case it's done like this:

    value = brush->polygon->what_ever;


    Or do you mean array of structs?

    for(int x = 0 ; x < 10 ; x++)
    brush->polygon[x] = (POLYGON *)malloc(sizeof(POLYGON));
    value = brush->polygon[x].what_ever;


    Or if you mean copying structs you need to copy whole memory area:

    memcpy((byte *)&brush->polygon[new], (byte *)&brush->polygon[old], sizeof(POLYGON));

    I don't know much about this memcpy stuff. This should work if the struct is allocated in continuous memory block. I think this works at least in Windows. Other way is to copy struct members one by one. If you have pointers inside struct you need to copy those separately. And remember to allocate memory before copying.


    : OK, I can now allocate mempry to all of the objects and their objects properly, but now, how do I go back and read one I previously set? Say I use brush once and setup a polygon in it, then reallocate ram to make a second one or third one, how would I go back and read the values of the first one?
    :
    :
    : -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red]
    :
    :




Sign In or Register to comment.