我有一个函数,我想把它作为一个参数,一个可变大小的二维数组。

到目前为止,我有这个:

void myFunction(double** myArray){
     myArray[x][y] = 5;
     etc...
}

我在代码的其他地方声明了一个数组:

double anArray[10][10];

然而,调用myFunction(anArray)会给我一个错误。

我不想在传入数组时复制它。在myFunction中所做的任何更改都应该改变anArray的状态。如果我理解正确,我只想传递一个指向2D数组的指针作为参数。该函数还需要接受不同大小的数组。比如[10][10][5][5]。我该怎么做呢?


当前回答

[10]不是指向指针的指针,它是一个连续的内存块,适合存储100个double类型的值,编译器知道如何寻址,因为你指定了维度。您需要将它作为数组传递给函数。你可以省略初始维度的大小,如下所示:

void f(double p[][10]) {
}

但是,这将不允许您传递除10之外的最后一个维度的数组。

c++中最好的解决方案是使用std::vector<std::vector<double> >:它几乎同样高效,而且明显更方便。

其他回答

#include <iostream>

/**
 * Prints out the elements of a 2D array row by row.
 *
 * @param arr The 2D array whose elements will be printed.
 */
template <typename T, size_t rows, size_t cols>
void Print2DArray(T (&arr)[rows][cols]) {
    std::cout << '\n';
    for (size_t row = 0; row < rows; row++) {
        for (size_t col = 0; col < cols; col++) {
            std::cout << arr[row][col] << ' ';
        }
        std::cout << '\n';
    }    
}

int main()
{
    int i[2][5] = { {0, 1, 2, 3, 4},
                    {5, 6, 7, 8, 9} };
    char c[3][9] = { {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'},
                     {'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R'},
                     {'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '&'} };
    std::string s[4][4] = { {"Amelia", "Edward", "Israel", "Maddox"},
                            {"Brandi", "Fabian", "Jordan", "Norman"},
                            {"Carmen", "George", "Kelvin", "Oliver"},
                            {"Deanna", "Harvey", "Ludwig", "Philip"} };
    Print2DArray(i);
    Print2DArray(c);
    Print2DArray(s);
    std::cout <<'\n';
}

我们可以使用几种方法将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; }

传递多维数组的一个重要的事情是:

第一个数组维度不需要指定。 必须指定第二个维度(任何其他维度)。

1.当全局只有第二个维度可用时(作为宏或全局常量)

const int N = 3;

void print(int arr[][N], int m)
{
int i, j;
for (i = 0; i < m; i++)
  for (j = 0; j < N; j++)
    printf("%d ", arr[i][j]);
}

int main()
{
int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
print(arr, 3);
return 0;
}

2.使用单个指针: 在此方法中,当传递给函数时,必须对2D数组进行类型转换。

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;

// We can also use "print(&arr[0][0], m, n);"
print((int *)arr, m, n);
return 0;
}

你可以使用c++中的模板功能来实现这一点。我是这样做的:

template<typename T, size_t col>
T process(T a[][col], size_t row) {
...
}

这种方法的问题在于,对于您提供的每一个col值,都会使用模板实例化一个新的函数定义。 所以,

int some_mat[3][3], another_mat[4,5];
process(some_mat, 3);
process(another_mat, 4);

实例化模板两次以生成2个函数定义(其中一个col = 3,另一个col = 5)。

令人惊讶的是还没有人提到这一点,但是您可以简单地在任何支持[][]语义的2D上创建模板。

template <typename TwoD>
void myFunction(TwoD& myArray){
     myArray[x][y] = 5;
     etc...
}

// call with
double anArray[10][10];
myFunction(anArray);

它适用于任何2D“类数组”数据结构,例如std::vector<std::vector<T>>,或者用户定义的类型以最大限度地重用代码。