Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何通过传递指针调用具有参数类型的高维数组的函数?_C++ - Fatal编程技术网

C++ 如何通过传递指针调用具有参数类型的高维数组的函数?

C++ 如何通过传递指针调用具有参数类型的高维数组的函数?,c++,C++,例如,我有一个函数(需要c99) 我有一个指针 double *p = new double[nx * ny]; 并使用它调用函数,如 fun(nx, ny, p); // error for the type is not matched 怎么做?允许进行任何类型转换。尝试以下操作: void fun(int nx, int ny, double** a){ // some code here // Get the element in this way: double myD = a[i

例如,我有一个函数(需要c99)

我有一个指针

double *p = new double[nx * ny];
并使用它调用函数,如

fun(nx, ny, p); // error for the type is not matched
怎么做?允许进行任何类型转换。

尝试以下操作:

void fun(int nx, int ny, double** a){
// some code here
// Get the element in this way: double myD = a[i][j];
}
总的来说:

double** a = new double*[nx];
for (int i = 0; i < nx; i++)
{
   a[i] = new double[ny];
}
或者,如果要传递数组:

void fun(int nx, int ny, double* a){
int dim = nx*ny;
double myD = a[i];
}

不可以,不能将一维数组(或指向一维数组的指针)传递给声明为采用二维数组的函数

如果需要动态分配二维阵列,可以执行以下操作:

// ny can be runtime variable in c99, but must be compile time constant in c++
constexpr int ny = 10;
int nx = 10;
double (*p)[ny]  = new double[nx][ny];
fun(nx, ny, p);
如果要按原样使用1d阵列,则需要修改
fun
以接受1d阵列:

// note that double a[] or double* a param would be identical, nx*ny is just for documentation
void fun(int nx, int ny, double a[nx*ny])
     // access indices [i][j] like this: a[nx*j + i]

在C++中,我建议您隐藏一个抽象的动态分配,它将管理内存。在C++中,C++是不可能的,因为C++要求数组类型的大小是编译时常数。C99没有此限制,因此函数声明

void fun(int nx, int ny, double a[nx][ny]);

是有效的C99,但不是有效的C++。顺便说一句,在C99中,此函数的正确调用如下所示:

int nx = ..., ny = ...;
double (*matrix)[ny] = malloc(nx*sizeof(*matrix));
fun(nx, ny, matrix);
double **matrix = new double*[nx];
for(int i = 0; i < ny; i++) matrix[i] = new double[ny];
现在,您有两种可能性:

  • 使用C来处理多维数组

  • < L> > P>使用C++解决方案。


    最简单的C++解决方案是<代码>向量< /代码>。这样可以避免自己分配内存的麻烦,但是,2D矩阵中的行不是连续的


    您还可以使用两层间接寻址,如下所示:

    int nx = ..., ny = ...;
    double (*matrix)[ny] = malloc(nx*sizeof(*matrix));
    fun(nx, ny, matrix);
    
    double **matrix = new double*[nx];
    for(int i = 0; i < ny; i++) matrix[i] = new double[ny];
    
    请注意,除了保存数据的数组之外,还需要一个额外的索引数组。但是,您可以自由使用单个大型阵列来保存数据:

    double** matrix = new double*[nx];
    double* storage = new double[nx*ny];
    for(int i = 0; i < ny; i++) matrix[i] = &storage[ny*i];
    

    这正是C99在引擎盖下所做的,代码在顶部,但类型检查要少得多。

    当然。指向double的指针从根本上讲与2D数组不同。如果将指针传递给一维数组,则无法实现这样的效果fun函数接受2D double数组,而将p定义为一维double数组。这是根本错误的。
    double** matrix = new double*[nx];
    double* storage = new double[nx*ny];
    for(int i = 0; i < ny; i++) matrix[i] = &storage[ny*i];
    
    void fun(int nx, int ny, double* twoDArray) {
        //access the array with
        twoDArray[x*ny + y];
    }
    
    //somewhere else
    double* matrix = new double[nx*ny];
    fun(nx, ny, matrix);