Create in_array
#1

Hi,

I want create a function.... look:

pawn Код:
// CMD
new array[1] = {FACT_TYPE_POLICE};
if(!in_array(array, pInfo[playerid][pP__faction])) return SendClientDialog(playerid, DEFAULT_TIME, "~r~Vous n'avez pas accиs а cette commande.");

// FUNCTION
stock in_array(array[], value)
{
    for(new i; i < sizeof(array); i++)
    {
        if(array[i] == value) return 1;
    }
    return 0;
}
indeterminate array size in "sizeof" expression (symbol "")

Idea ?
Thanks
Reply
#2

pawn Код:
stock in_array(array[], value, size = sizeof array)
{
    for(new i; i < size; i++)
    {
        if(array[i] == value) return 1;
    }
    return 0;
}
Why? Because sizeof is resolved at compile time, not run time. You have to pass the size when you call the function. You might be interested with custom iterators from y_iterate/foreach. But that's for bigger dynamic arrays. Also, you could simply return index at the same time, with -1 when not found.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)