Q:

How to call a function with a parameter typed high-dimensional array by passing a pointer?

For example, I have a function like (need c99)
void fun(int nx, int ny, double a[nx][ny])
{
    // some code here
}

and I have a pointer
double *p = new double[nx * ny];

and use it to call the function like
fun(nx, ny, p); // error for the type is not matched

How to do it? Any type conversion is allowed.

A:

What you want is not possible in C++ because C++ requires the sizes of array types to be compile time constants. C99 does not have this limitation, so the function declaration
void fun(int nx, int ny, double a[nx][ny]);

is valid C99, but not valid C++. Btw, in C99, the correct call of this function would look like this:
int nx = ..., ny = ...;
double (*matrix)[ny] = malloc(nx*sizeof(*matrix));
fun(nx, ny, matrix);

Now, you have two possibilities:

Use C for your multidimensional array stuff.
Use a C++ workaround for this.

The easiest C++ workaround would be a vector<vector<double> >. With that you avoid the hassle of allocating the memory yourself, however, the rows in your 2D matrix are not consecutive.

You can also use a two layer indirection like this:
double **matrix = new double*[nx];
for(int i = 0; i < ny; i++) matrix[i] = new double[ny];

and declare your function as
void fun(int nx, int ny, double** a);

Note that you need one additional index array in addition to the arrays that hold the data. However, you are free to use a single large array to hold the data:
double** matrix = new double*[nx];
double* storage = new double[nx*ny];
for(int i = 0; i < ny; i++) matrix[i] = &storage[ny*i];

The final possible workaround is, to do the index calculation yourself:
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);

This is exactly what C99 does under the hood with the code at the top, but with significantly less type checking.

