Ç - 传递一个二维数组作为函数参数? [英] C -- passing a 2d array as a function argument?

查看:165
本文介绍了Ç - 传递一个二维数组作为函数参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个可以轻松定义一个接受一维数组参数这样的功能:

One can easily define a function that accepts a 1d array argument like this:

int MyFunction( const float arr[] )
{    
    // do something here, then return...

    return 1

}

尽管定义如:
INT MyFunction的(常量浮动* ARR)将正常工作。

一个人怎么可以定义一个接受二维数组参数的函数?

How can one define a function that accepts a 2d array argument?

我知道这工作:
INT MyFunction的(常量浮动** ARR) - 但是,是否有可能使用使用第一个变化 []

I know that this works: int MyFunction( const float** arr ) -- but, is it possible to use the first variation that uses []?

推荐答案

在C99中,您可以传递之前,提供阵列的尺寸:

In C99, you can provide the dimensions of the array before passing it:

 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);
 }

这篇关于Ç - 传递一个二维数组作为函数参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆