Assigning Arrays

Monkey Forums/Monkey Programming/Assigning Arrays

darky000(Posted 2013) [#1]
Hi,

Is there a way to assign the starting array address to another variable? If it can, how can I do that? I have a lot of arrays and I am picking one that is to be used in certain situations. I want to avoid repeating line of codes or using select every time.

In cpp you can assign something like this:

int *arrayptr;
int sample[10];
int sampleAgain[5];

arrayptr = sample; 'where sample holds the address of index-0



Goodlookinguy(Posted 2013) [#2]
Monkey doesn't have pointers, so no.


darky000(Posted 2013) [#3]
I see... but can it at least be referenced?


Goodlookinguy(Posted 2013) [#4]
What be referenced? The memory address of the array? If that's what you're asking about, then no.

If on the other hand you're asking: If 'a' is an array datatype and 'b' is an initiated array when 'a' assigns 'b', 'a' becomes a reference to 'b'. Then the answer is yes.

Function Main:Int()
    Local a:Int[]
    Local b:Int[] = [0]
    
    a = b
    a[0] = 1
    
    Print b[0]
End



darky000(Posted 2013) [#5]
That's a good alternative. Thanks Goodlookingguy.

P.S. Your name makes me look like I'm flirting. -_-"


Gerry Quinn(Posted 2013) [#6]
You can pass arrays around, but you can't re-assign them (without losing the reference).


Local a:Int[] = [ 3, 4, 5 ]
DoSomething( a )

Function DoSomething( arr:Int[] )

' This works to change a to [ 3, 10, 5 ]
arr[ 1 ] = 10


' This has no effect on a
arr = [ 3, 20, 5 ]

End




The second line in DoSomething doesn't affect a because arr is set to point to a completely new array (which will be forgotten about when the function returns). The first line works because arr points to the original array a.