Page 1 of 2

How to reset all values in an array

PostPosted: Wed May 23, 2012 2:33 pm
by Hblade
If you want to reset an array and all of it's values, you first need to know the name of your array you are trying to reset. Lets say we have an array called gameArrays[259]. Place this code in the Global Code area
Code: Select all
void
gameArraysReset()
{
    int i;
    for(i=0;i<258;i++)
    {
        gameArrays[i]=NULL;
    }
}


This should work, now once you execute the command gameArraysReset(); you should be able to reset every value in the array.

Note:
The name of the function is the same as the variable array. gameArraysReset is designed to reset that specific array.

Re: How to reset all values in an array

PostPosted: Fri May 25, 2012 11:59 am
by Troodon
Slightly off topic but can the i<258; part be replaced by i<gameArrays.length?

Re: How to reset all values in an array

PostPosted: Fri May 25, 2012 12:47 pm
by schnellboot
and also it has to be i<=258 because you know the last index is 258 ^^

Re: How to reset all values in an array

PostPosted: Fri May 25, 2012 12:56 pm
by Hblade
mhm :)
Also, if you were to use something like that you'd have to do .length-1 xD Maybe.. or not though :D

Re: How to reset all values in an array

PostPosted: Fri May 25, 2012 1:19 pm
by Troodon
Yeah, lenght-1 of course. My bad :oops:

Re: How to reset all values in an array

PostPosted: Fri May 25, 2012 1:34 pm
by SuperSonic
Wait, so if you have an array like, int myarray[7]; and you want to get it's size, you can just do myarray.length?

Re: How to reset all values in an array

PostPosted: Fri May 25, 2012 1:53 pm
by Hblade
I havent a clue xD

Re: How to reset all values in an array

PostPosted: Fri May 25, 2012 2:20 pm
by SuperSonic
Haha, ok :lol:

Re: How to reset all values in an array

PostPosted: Fri May 25, 2012 3:55 pm
by skydereign
C doesn't use classes for arrays, so you can't use arrayName.length like you might in other languages. If you really wanted to do that you could create your own derived type for the array, some structure that contained your array and your length count. And it wouldn't be length-1 anyways, since you are handling <. The length of that array is 258 (as the values are 0-257). If it were <= you would need a -1.

Re: How to reset all values in an array

PostPosted: Fri May 25, 2012 6:13 pm
by Troodon
Finally, answer to my question. :) Thanks

Re: How to reset all values in an array

PostPosted: Fri May 25, 2012 8:09 pm
by schnellboot
sky, actually the length of that array is 279 so it is 0-278 ^^

Re: How to reset all values in an array

PostPosted: Fri May 25, 2012 8:32 pm
by skydereign
Ah, I didn't read the actual first post. I just assumed it was 0-277 since people were asking about the length -1. Either way though, the length of an array is its last value, which is the value you use in the for loop (since the standard is < instead of <= in for loop).

Re: How to reset all values in an array

PostPosted: Sat May 26, 2012 3:11 pm
by Hblade
Ya learn something new every day! :)

Re: How to reset all values in an array

PostPosted: Sun May 27, 2012 8:58 pm
by lcl
Hey Hblade (and others as well) you might want to have a look at this: viewtopic.php?f=5&t=11052#p76692
It's an array resetting function made by me. You just write the name of the array you want to reset and the size of it, or the limit to how far you want it to be limited and it does it all for you! :D

Re: How to reset all values in an array

PostPosted: Sun May 27, 2012 9:12 pm
by Hblade
Very nice! :D