Passing dynamic arrays
bearophile
bearophileHUGS at lycos.com
Tue Nov 9 05:03:22 PST 2010
Steven Schveighoffer:
> I think of an array as a hybrid between a reference and a value type. The
> data is passed by reference, the length is passed by value. This mean
> changing the length only affects the local copy, but changing the data
> affects all arrays that point to that data.
Let's play some more :-)
The data is passed by pointer value, the length is passed by value:
void foo1(ref int[2] arr) {
int[2] a = [10, 20];
arr = a;
}
void foo2(int[] arr) {
int[2] a = [10, 20];
arr = a;
}
void main() {
int[2] arr;
arr = [1, 2];
foo1(arr);
assert(arr == [10, 20]);
arr = [1, 2];
foo2(arr);
assert(arr == [1, 2]);
}
Bye,
bearophile
More information about the Digitalmars-d
mailing list