Passing a Pointer to an Array to a Function
Monkey Forums/Monkey Programming/Passing a Pointer to an Array to a Function
| ||
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? |
| ||
Array declarations are references/pointers in Monkey. You can't do anything other than pass an array reference. |
| ||
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 |
| ||
Double post deleted. |
| ||
TY, gents. That was basically what I thought but every language handles it differently... |
| ||
Wait. YOU CAN PASS RGB VALUES AS A SINGLE STRING? |
| ||
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. |