可以轻松定义一个接受这样的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 )- 但是,是否可以使用第一个使用的变体[]?
启发了这种静止.
为什么 -
void display(int p[][SIZE]) // allowed
Run Code Online (Sandbox Code Playgroud)
和
void display(int p[][]) // not allowed
Run Code Online (Sandbox Code Playgroud)
?
我们可以将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)