我有一个二维数组,我需要一次处理一列。我写了一个示例代码来说明我想做什么。它显然不会编译。
float a[3][16]; // Create 2D array
void function1() // This function will be called from my application
{
for (int i=0; i<16; i++) // For each column of the 2D array "a"
{
// Call this function that only take 1D array parameters
function2(a[][i]); // What I want is all rows in column i
// MATLAB syntax is: function2(a(:,i));
}
}
void function2(float b[])
{
// Something
}
我知道我可以制作一个临时数组,将每一列保存到其中并将其用作 function2 中的参数。我想知道是否有更好的方法或者你会怎么做?
最佳答案
最好的方法是将整个二维数组连同选择列的参数一起传递给 function2()。然后沿着轴迭代。
for (int i=0; i<16; i++) // For each column of the 2D array "a"
{
function2( a , i );
}
void function2(float b[Y][X] , size_t col )
{
for( size_t i = 0 ; i < Y ; i++ )
b[i][col] = ...
}
关于c - 使用矩阵作为一维数组参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23543841/