【问题标题】:Passing 2-D array to function in c [duplicate]将二维数组传递给c中的函数[重复]
【发布时间】:2013-01-18 01:59:45
【问题描述】:

可能重复:
Passing multidimensional arrays as function arguments in C

在 C 中, 如果我想让一个函数接收一个二维数组,我可以在函数参数中使用 * 符号吗

int (int my2dary[][10]);  //This is what I do not want.

【问题讨论】:

    标签: c arrays pointers


    【解决方案1】:

    如果您的问题是在编译时不知道数组的大小,您可能需要:

    int func(int *array, int size)
    {
       int n,m;
       ... 
       array[m*size+n]; /* = array[m][n] if in the caller: int array[x][size]; */
    }
    

    您可以选择(并且很可能您需要)传递第二个大小参数 (x) 来测试数组边界

    【讨论】:

      【解决方案2】:

      是的,你传递了一个指向 int 数组的指针

      int func(int (*my2dary)[10]);
      

      你叫它

      int a[5][10];
      func(a);
      

      虽然func不知道my2dary中有多少元素,所以你也必须给出一个数字

      int func(int n, int (*my2dary)[10]);
      

      并调用

      int a[5][10];
      func(5, a);
      

      How to interpret complex C/C++ declarationsThe ``Clockwise/Spiral Rule''

      【讨论】:

        猜你喜欢
        • 2013-03-28
        • 1970-01-01
        • 2018-12-08
        • 2021-05-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多