SA-MP Forums Archive
Multi dimensional arrays passed into a function - 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)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Multi dimensional arrays passed into a function (/showthread.php?tid=217445)



Multi dimensional arrays passed into a function - rannandaman - 27.01.2011

I currently have an array such as this:

Code:
new Float:Hello[6][2] = {
    {1234.1211,1854.0146},
    {1564.4543,1564.1547},
    {1224.4215,1511.1113},
    {1133.1123,1112.4148},
    {1134.8743,1644.1546},
    {1154.4445,2124.1544}
};
(Note that all values are just random for the purpose of this post)


How do I call it in a function and then loop through all the values, such as:

Code:
public Random(Hello) {
}
I have tried defining it as

Code:
public Random(Float:RandomVariable[][]) {  
}
The above does allow me to pass the array into the function, however since there is no definate size of the passed array, it causes errors when trying to read the values with a loop. Since, I can't use sizeof(), I don't know how I can read all of the values Of an undetermined array size.

If I make it loop through a set number of times:

Code:
public Random(Float:RandomVariable[][]) {
	for(new i=0;i < 10;i++) {
		printf("%d - %f,%f",i,RandomVariable[i][0],RandomVariable[i][1]);
	}
	print("HELLO");
}
the server crashes when i == 6

Basically, How do I get all the values from a multi dimensional array when the array that is passed can differ in size?


Re: Multi dimensional arrays passed into a function - Krx17 - 27.01.2011

Try this
pawn Code:
new Float:Hello[6][2] = {
    {1234.1211,1854.0146},
    {1564.4543,1564.1547},
    {1224.4215,1511.1113},
    {1133.1123,1112.4148},
    {1134.8743,1644.1546},
    {1154.4445,2124.1544}
};

stock myFunction(Float:myArray[][], myArraySize)
{
    for(new i=0; i < myArraySize; i++)
    {
        printf("%d - %f,%f",i,myArray[i][0],myArray[i][1]);
    }
    return 1;
}



Re: Multi dimensional arrays passed into a function - Rachael - 27.01.2011

add another parameter to the function that refers to the array size


Re: Multi dimensional arrays passed into a function - rannandaman - 27.01.2011

Of course.

Code:
Random(Hello,sizeof(Hello));
I feel stupid for not thinking of that, now. Thanks