我有这个用于从文件中读取数据的 D 代码:
int foo (SomeStruct[] somestruct_array)
{
// get file_handle...
for (uint i = 0; i < 1000; i++) {
file_handle.readf ("%e %e %e %e %e %e\n",
&(somestruct_array[i].pos_x), &(somestruct_array[i].pos_y),
&(somestruct_array[i].pos_z), &(somestruct_array[i].vel_x),
&(somestruct_array[i].vel_y), &(somestruct_array[i].vel_z));
}
return 0;
}
如你所见,台词越来越长,我一直在重复somestruct_array[i]
。这没什么大不了的,但我试图缩短它并写下:
int foo (SomeStruct[] somestruct_array)
{
// get file_handle...
for (uint i = 0; i < 1000; i++) {
auto elem = somestruct_array[i];
file_handle.readf ("%e %e %e %e %e %e\n",
&(elem.pos_x), &(elem.pos_y),
&(elem.pos_z), &(elem.vel_x),
&(elem.vel_y), &(elem.vel_z));
}
return 0;
}
这导致所有元素成员都是nan
.
那么:是否可以获得对数组元素的引用?