Skip to main content
gamedev.net
🔒 Locked

Really dumb pointer question

Started by Ataru Jan 25, 2003 at 9:36 AM 3 replies 650+ views
Original Post
Ataru
Ataru
I''ve never had problems with pointers before, but for some dumb reason this doesn''t work, and I''m a little tired of it. Here''s what I have, in an easy to read example. objects *gameObj[MAX_OBJ]; objects *selected; void foo() { selected = NULL; select(selected); } void select(objects * selected) { for (int i = 0; i < MAX_OBJ; i++) { if (obj_selected) selected = gameObj; } } Basically I want selected to point to the selected object, whichever it is. Before calling select() selected is NULL, inside select(), selected is not NULL, but after returning selected is NULL again. Now if I understand my pointers, when selected points to the smae location, unless I make gameObj[i] NULL, it should point to that object always. It''s probably one dumb thing I''m missing, but for the life of me, I can''t figure out what it is. Thanks
kuphryn
kuphryn
gameObj is an array of points. Inside select, you set selected to gameObj. Set selected to one object in the array.

Kuphryn
HashMaster
HashMaster
As far as I can see the problem is that the pointer 'selected' that you set to NULL in foo() and the pointer 'selected' that you use as the parametre for select() are two separate entities...what you need to do I think is pass by reference...ie: void select(object* &selected)...


[edited by - HashMaster on January 25, 2003 12:41:05 PM]
"...we can't stop here! This is bat country!" - Hunter S Thompson
Ataru
Ataru
It''s not really global in my implementation. Just needed to declare it as an example. Thanks, master, that should work. I''m going to go test it now. I figured that was the problem, just didn''t know how to solve it.

Thanks again

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.