Multi dimensional arrays passed into a function
#1

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?
Reply
#2

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;
}
Reply
#3

add another parameter to the function that refers to the array size
Reply
#4

Of course.

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


Forum Jump:


Users browsing this thread: 1 Guest(s)