// a is 2d --- n x n matrix
void f(int** a, int n); // (1)
int main()
{
const int n=3;
int a[n][n]; // (2)
/**
Question: how do I pass the matrix a to f()?
Don't modify lines (1) and (2).
Thanks.
*/
return 0;
}
1. 野比's soln does not work, test the program below yourself:
[CODE]#include <iostream>
using namespace std;
void f(int** a, int n)
{
int i;
int j;
for(i=0; i<n; ++i)
for(j=0; j<n; ++j)
a[i][j] = i+j;
}
int main(int argc, char** argv)
{
const int n=3;
int a[n][n];
int* b = &(a[0][0]);
int** c = &b;
f(c, n);
return 0;
}[/CODE]
2. tobyliying --- I just want to understand a C++ syntax.
3. I wrote the function f(), assuming that I will pass a dynamically allocated 2d array to it. Then I changed idea --- I just want to pass a 2d array on the program stack (instead of the heap).
Your suggestion is good --- but I don't know n beforehand. Thus "pointer to array of lenght n" approach is not appropriate.