我有一个函数,我想把它作为一个参数,一个可变大小的二维数组。
到目前为止,我有这个:
void myFunction(double** myArray){
myArray[x][y] = 5;
etc...
}
我在代码的其他地方声明了一个数组:
double anArray[10][10];
然而,调用myFunction(anArray)会给我一个错误。
我不想在传入数组时复制它。在myFunction中所做的任何更改都应该改变anArray的状态。如果我理解正确,我只想传递一个指向2D数组的指针作为参数。该函数还需要接受不同大小的数组。比如[10][10][5][5]。我该怎么做呢?
如果你想将int的[2][3]传递给void func(int** pp),你需要以下辅助步骤。
int a[2][3];
int* p[2] = {a[0],a[1]};
int** pp = p;
func(pp);
由于第一个[2]可以隐式指定,因此可以进一步简化为。
int a[][3];
int* p[] = {a[0],a[1]};
int** pp = p;
func(pp);
你可以像这样创建一个函数模板:
template<int R, int C>
void myFunction(double (&myArray)[R][C])
{
myArray[x][y] = 5;
etc...
}
然后你通过R和c有两个维度大小,每个数组大小都会创建一个不同的函数,所以如果你的函数很大,并且你用各种不同的数组大小调用它,这可能代价很高。你可以像这样使用它作为一个函数的包装器:
void myFunction(double * arr, int R, int C)
{
arr[x * C + y] = 5;
etc...
}
它将数组视为一维,并使用算术计算出索引的偏移量。在这种情况下,你可以这样定义模板:
template<int C, int R>
void myFunction(double (&myArray)[R][C])
{
myFunction(*myArray, R, C);
}
如果你想将int的[2][3]传递给void func(int** pp),你需要以下辅助步骤。
int a[2][3];
int* p[2] = {a[0],a[1]};
int** pp = p;
func(pp);
由于第一个[2]可以隐式指定,因此可以进一步简化为。
int a[][3];
int* p[] = {a[0],a[1]};
int** pp = p;
func(pp);
我们可以使用几种方法将2D数组传递给函数:
Using single pointer we have to typecast the 2D array.
#include<bits/stdc++.h>
using namespace std;
void func(int *arr, int m, int n)
{
for (int i=0; i<m; i++)
{
for (int j=0; j<n; j++)
{
cout<<*((arr+i*n) + j)<<" ";
}
cout<<endl;
}
}
int main()
{
int m = 3, n = 3;
int arr[m][n] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
func((int *)arr, m, n);
return 0;
}
Using double pointer In this way, we also typecast the 2d array
#include<bits/stdc++.h>
using namespace std;
void func(int **arr, int row, int col)
{
for (int i=0; i<row; i++)
{
for(int j=0 ; j<col; j++)
{
cout<<arr[i][j]<<" ";
}
printf("\n");
}
}
int main()
{
int row, colum;
cin>>row>>colum;
int** arr = new int*[row];
for(int i=0; i<row; i++)
{
arr[i] = new int[colum];
}
for(int i=0; i<row; i++)
{
for(int j=0; j<colum; j++)
{
cin>>arr[i][j];
}
}
func(arr, row, colum);
return 0;
}