Skip to main content
GameDev.net gamedev.net
🔒 Locked

help with pointers

Started by r3n3gad3 Jul 17, 2003 at 7:57 AM 2 replies 250+ views
Original Post
r3n3gad3
r3n3gad3
hi... i am trying to make a tile sliding game and i wanted to create an array of pointers to point to an array of objects... the point of this was to say the random number random thingo put the number 7 in array[2] then pntrarray[7] would point to this array and when the user says to slide that pntrarray then all i would have to do is pntrarray[x] = last;... which is really easy... i tried to do this in the following code int array[9]; int *pntrarray = new int [9]; for (int i=0; i < 9; i++) { now: num = rand() % 9; if (!chknum()) { //this function checks to see if there is //no array with the same number array = num pntrarray[num] = & array[i] }else{ goto now; } }
nickwinters
nickwinters
Not sure what you''re asking for help with, but here''s some advice for general programming.

Avoid goto statements. They make it hell to debug. Here''s a way to avoid the goto statement

for (int i=0; i < 9; i++)
{
num = rand() % 9;
if (!chknum())
{
//this function checks to see if there is
//no array with the same number
array = num
pntrarray[num] = & array
}
else
{
i--;
continue;
}
}

This way, it will go to the top of the loop, and the increment and decrement will negate each other.
Trienco
Trienco
quote:
Original post by nickwinters
Not sure what you''re asking for help with, but here''s some advice for general programming.

Avoid goto statements. They make it hell to debug. Here''s a way to avoid the goto statement


though one could argue if modifying the counter inside a for loop is that much better. why not use a while loop in the first place?

while (i < 9){
num = rand() % 9;
if (!chknum())
{
array = num;
pntrarray[num] = & array[i++];
}
f@dzhttp://festini.device-zero.de
civguy
civguy
quote:
Original post by r3n3gad3
.. i tried to do this in the following code
..And you didn't explain what the problem is.

Anyway, since it's so popular, here's another variation of your code.

int array[9];
int** pntrarray = new int*[9]; //array of int pointers if probably what you want

for (int j = 0; j < 9; j++)
{
while (chknum(num = rand() % 9)); //I thought maybe chknum needs to take a parameter too..
array[j] = num;
pntrarray[num] = &array[j];
}




[edited by - civguy on July 17, 2003 1:19:08 PM]

Topic Locked

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

Sign in to reply to this topic.