SA-MP Forums Archive
sizeof through an array - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: sizeof through an array (/showthread.php?tid=482587)



sizeof through an array - 2KY - 21.12.2013

I have this enum;

pawn Код:
enum pVars
{
    Password[129],
    Admin,
   
    WeaponKills[46]
}
new
    PlayerInfo[MAX_PLAYERS][pVars],
    bool: is_logged[MAX_PLAYERS];
Pretty standard, but I'm trying to access the amount of cells in "WeaponKills" which is currently 46.

I was going to do it like this;

pawn Код:
for(new i; i < sizeof(PlayerInfo[playerid][WeaponKills]); i++)
{
}
But obviously since I'm here, it doesn't work. How would I go about doing that?


Re: sizeof through an array - Konstantinos - 21.12.2013

You cannot use sizeof with enums so use the size instead.

pawn Код:
for(new i; i < 46; i++)



Re: sizeof through an array - Pottus - 21.12.2013

What Konstantinos said but I'm going to add this when these sort of situations come up use a #define of course.

#define WEAPON_KILL_SIZE 46


Re: sizeof through an array - Patrick - 21.12.2013

Just going to combine what Konstantinos and [uL]Pottus said :P

pawn Код:
#define WEAPON_KILL_SIZE (46)

enum pVars
{
    Password[ 129 ],
    Admin,
    WeaponKills[ WEAPON_KILL_SIZE ]
}

new
    PlayerInfo[ MAX_PLAYERS ][ pVars ],
    bool: is_logged[ MAX_PLAYERS ];
Loop
pawn Код:
new i = 0;
while(i < WEAPON_KILL_SIZE)
{
    //Your code.
    i++;
}

// or

for(new i = 0; i < WEAPON_KILL_SIZE; i++)
{
    //Your code
}



Re: sizeof through an array - 2KY - 21.12.2013

Thanks guys, wasn't sure if there was another way to do it. Had that way as a placeholder anyway - thanks again.