Pointers

Started by
5 comments, last by Ataru 21 years, 1 month ago
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.
Advertisement
Maybe you should just declare your insert function like this:

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

without the & .
--Ben CarterRomans 8:38-39
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.
struct node{game_objects *inserted[5];int numObj=0;};//Node is properly initialized, memory is allocatedvoid 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.

Beginner in Game Development?  Read here. And read here.

 

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.
Take a look at this. it may might drop a lightbulb

also have you tried this:

&(tst)->inserted[0] = toInsert;

try it with and without the parenthesis.

hope you figure this out soon.

Beginner in Game Development?  Read here. And read here.

 

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

This topic is closed to new replies.

Advertisement