您不能使用变量指定矩阵的大小。此外,这里没有命令将清除矩阵并释放它使用的内存。
在调用点,使用相同类型的数组变量。或者,您可以使用数组表达式注释来创建数组,而无需声明变量并执行所有指定到它那里。
proc foo ( float $f[], string $s[]) {
print("size of f=" + size($f) + "\n");
for ( $i=0; $i < size($f); ++$i ) {
print("f[" + $i + "]=" + $f[$i] + "\n");
}
print("size of s=" + size($s) + "\n");
for ( $i=0; $i < size($s); ++$i ) {
print("s[" + $i + "]=" + $s[$i] + "\n");
}
}
float $ff[2]={0.9, 1.2};
float $gg[];
for ( $i=0; $i < 10; ++$i ) {
$gg[$i] = $i;
}
foo $ff {}; // passes the array "$ff" and the empty array to foo.
foo $gg {"hello", "world"}; // passes the array "$gg" and an array of 2 strings
// to foo.
foo {} {}; // calls foo with 2 empty arrays.
数组表达式从列表中的第一个元素获得它们的基本类型。因此,要强制使数组表达式为某个特定类型,您可以投射第一个元素:
foo {(float)1, 2, 3} {"hello"};
// make first array an array of float, not int.