相关疑难解决方法(0)

C - 传递2d数组作为函数参数?

可以轻松定义一个接受这样的1d array参数的函数:

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

    return 1

}
Run Code Online (Sandbox Code Playgroud)

虽然诸如以下的定义 int MyFunction( const float* arr )也可以起作用.

如何定义接受2d array参数的函数?

我知道这有效: int MyFunction( const float** arr )- 但是,是否可以使用第一个使用的变体[]

c

10
推荐指数
2
解决办法
7万
查看次数

3
推荐指数
1
解决办法
412
查看次数

传递2d数组函数时单指针和双指针有什么区别?

我们可以将2d数组作为单个指针传递,也可以传递双指针.但在第二种情况下,输出不如预期.那么第二个代码有什么问题?

方法1:

#include <stdio.h>
void print(int *arr, int m, int n)
{
    int i, j;
    for (i = 0; i < m; i++)
      for (j = 0; j < n; j++)
        printf("%d ", *((arr+i*n) + j));
}

int main()
{
    int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    int m = 3, n = 3;
    print((int *)arr, m, n);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

1 2 3 4 5 6 7 8 9
Run Code Online (Sandbox Code Playgroud)

方法2:

#include <stdio.h>
void …
Run Code Online (Sandbox Code Playgroud)

c arrays pointers function

3
推荐指数
1
解决办法
271
查看次数

标签 统计

c ×3

arrays ×2

pointers ×2

function ×1

multidimensional-array ×1