C——传递一个二维数组作为函数参数?

标签 c

可以很容易地定义一个接受 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/

相关文章:

我可以比较函数指针和函数是否相等吗?

c - 在二叉树中插入字符串

c - sscanf 在我的字符串中添加 0

c - 汇编程序在调用 printf 后崩溃

创建一个简单的动态库

c - 错误 : assignment makes integer from pointer without a cast (using gets)

将 Haskell 程序编译为 C

c - C 中的 pthreads - pthread_exit

c - 为什么在我尝试分配新内存时 realloc 会使我的程序崩溃?

c - 流式细胞术 FCS 文件数据段,线性数据似乎有偏差