Array w/ Strings Loop
#1

If I have an array, such as:
Код:
new myArray[MAX_PLAYERS][10][50];
, which is supposed to hold 10 strings. How do I loop through it to check for an open slot? I know how to loop through an array that contains number values, but not string values. I have tried using strcmp and strlen, but without success.
Reply
#2

pawn Код:
stock GetFreeSlot( playerid )
{
    for( new i; i != sizeof( myArray[ ] ); i++ )
    {
        if( !myArray[ playerid ][ i ][ 0 ] ) return i;
    }
    return -1;
}
It returns the free slot or -1 if there is not any. Note that if you're going to use the value returned as an index, you must check if it's not -1 first to prevent runtime error: Index out of bounds.
Reply
#3

Thanks for the response! So if I wanted to add a string to a free slot, I would do something like:
Код:
myArray[playerid][GetFreeSlot(playerid)] = "My string here";
Reply
#4

Yes, but check the returned value first to avoid runtime errors.

pawn Код:
new slot = GetFreeSlot(playerid);
if(slot != -1) myArray[playerid][slot] = "My string here";
So if it returned 1 because the 0 was not null, then the myArray[playerid][1] will be "My string here".
Reply
#5

Nice, thanks for your help!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)