Passing a Pointer to an Array to a Function

Monkey Forums/Monkey Programming/Passing a Pointer to an Array to a Function

zoqfotpik(Posted 2013) [#1]
How can I pass a pointer to a 2D array to a function?

I want to create a generalized, simple 2D graphics library that instead of plotting to a pixmap, plots to 2D arrays.

Thoughts?


muddy_shoes(Posted 2013) [#2]
Array declarations are references/pointers in Monkey. You can't do anything other than pass an array reference.


Gerry Quinn(Posted 2013) [#3]
The only thing to remember is not to reallocate the array inside your function, or the reference will no longer point to the original:

' The array I passed gets a red dot
Function DoStuff:Void( pixels:Int[][] )
	pixels[ 10 ][ 10 ] = $FFFF0000
End Function

' Nothing happens to the array I passed
Function DoStuff:Void( pixels:Int[][] )
	pixels = New Int[ 200 ][ 100 ]
	pixels[ 10 ][ 10 ] = $FFFF0000
End Function



Gerry Quinn(Posted 2013) [#4]
Double post deleted.


zoqfotpik(Posted 2013) [#5]
TY, gents. That was basically what I thought but every language handles it differently...


zoqfotpik(Posted 2013) [#6]
Wait. YOU CAN PASS RGB VALUES AS A SINGLE STRING?


muddy_shoes(Posted 2013) [#7]
As an array of Ints/Floats, you mean? Yes.

Be aware that something like SetRGB([r,g,b]) may not be very performance friendly though.