13.03.2012, 14:43
Is there a way to reference an array?
public OnGameModeInit()
{
new arr[10];
ProcessMyArray(arr);
}
public ProcessMyArr(array[])
{
//Do something...
}
Hi T0pAz,
Ok, there is no need to use the & symbol in this instance. So if you want to pass an array to a function, the following would be a good example: pawn Код:
Cheers, TJ |
return hello;
public doIt(str[],length)
{
new stuff = 1;
return format(str,length,"%d",stuff);
}
public OnPlayerConnect(playerid)
{
new str[32];
doIt(str,32);
}
#include <a_samp>
public OnFilterScriptInit()
{
new myINT = 47, Float:myFLT = 2.4751;
DoubleUp_NoRef( myINT, myFLT );
printf("%i %f", myINT, myFLT);
//prints: 47 2.4751
//No changes to our local variables.
DoubleUp( myINT, myFLT );
printf("%i %f", myINT, myFLT);
//prints: 94 4.95
//The values of our local variables has changed.
new myARRAY[32] = "sometext";
DoubleUpArray( myARRAY );
printf("%s", myARRAY);
//prints: sometextsometext
//Even though the Function didn't have referrence arrays
//The local variable has changed it's content.
return true;
}
stock DoubleUp( &INT, &Float:FLT )
{
INT *= 2;
FLT *= 2;
return true;
}
stock DoubleUp_NoRef( INT, Float:FLT )
{
INT *= 2;
FLT *= 2;
return true;
}
stock DoubleUpArray( Array[], size = sizeof Array )
{
strcat(Array, Array, size);
return true;
}
You don't need to reference arrays..
Because, In simple words, when you pass an array to a function, the changes occur to the same variable (mem address). But for Ints, Floats, when you pass to a function, they don't change themselves, they transfer their 'values'. So, if you want to make changes to their actually values, you need to pass them as references.. pawn Код:
|