可以很容易地定义一个接受 1d array
参数的函数,如下所示:
int MyFunction( const float arr[] )
{
// do something here, then return...
return 1
}
虽然定义如下:
int MyFunction( const float* arr )
也可以。
如何定义接受二维数组
参数的函数?
我知道这行得通:
int MyFunction( const float** arr )
-- 但是,是否可以使用第一个使用 []
的变体?
最佳答案
在 C99 中,您可以在传递数组之前提供数组的维度:
void array_function(int m, int n, float a[m][n])
{
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
a[i][j] = 0.0;
}
void another_function(void)
{
float a1[10][20];
float a2[15][15];
array_function(10, 20, a1);
array_function(15, 15, a2);
}
关于C——传递一个二维数组作为函数参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6862813/