• Advertisement

Archived

This topic is now archived and is closed to further replies.

Pointers

This topic is 5425 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Maybe I''m just tired but I can''t seem to get this to work. Here''s some pseodocode of my problem. File 1. game_object *items[1000]; foo() { insert(items[0]); } File 2 struct node { game_objects *inserted[5]; int numObj=0; }; //Node is properly initialized, memory is allocated void insert(game_object* &toInsert) { tst->inserted[0] = toInsert; printf("%d %d",toInsert->x,toInsert->y); } When I try to acess toInsert (via the printf) the program crashes. For some reason tst->inserted[0] does not point to the same address as items[0] (which is what I want). Maybe I''ll figure it out in the morning, but I always get these stupid pointer things wrong. Any help is appreciated.

Share this post


Link to post
Share on other sites
Advertisement
Maybe you should just declare your insert function like this:

void insert(game_object * toInsert)
{
.
.
.
}

without the & .

Share this post


Link to post
Share on other sites
No, that''s what I tried first. When it didn''t work I figured I had to send the pointer to the address but still no go.

Share this post


Link to post
Share on other sites

struct node
{
game_objects *inserted[5];
int numObj=0;
};

//Node is properly initialized, memory is allocated
void insert(game_object* &toInsert)
{
tst->inserted[0] = toInsert;
printf("%d %d",toInsert->x,toInsert->y);
}


it should be (i think)

void insert(game_objects **toInsert)
{
tst->inserted[0] = **toInsert; // assigning values not addresses
printf("%d %d", *toInsert->x, toInsert->y);
}


i don''t have my book in front of me but i think that''s 80% right,
i think.

Share this post


Link to post
Share on other sites
Still a no go. You have to pass the address of course so it would be:

insert(&items[0]);

insert(game_node **obj) ...

but the same thing happens. I don''t want to pass the value. I just want an array of pointers pointing to instantiated objects. Ugh, it should work.

Share this post


Link to post
Share on other sites
Or I could have really been tired (I wasn''t kidding) and not realized that I was actually passing the equiv of items[0+1] and therefore passing a NULL pointer to insert.

Lol, thanks for the help everyone

Share this post


Link to post
Share on other sites

  • Advertisement