"Klaas Vantournhout" <no************@spam.com> wrote in message
news:dv**********@gaudi2.UGent.be
Hi all,
I'm in need of a matrix of function pointers, and to be honest. No
'nice' solution has been found yet on that big big internet.
It is possible to declare a matrix of function pointers in the
following way
void (*f[2][2])(int);
But I want to know how I can declare this only using double pointers.
like an array
double **a;
a = new double *[2];
for (int i = 0; i < 2; i++)
a[i] = new double [2];
I would like to know this, because I have in my code something which
is similar like below, and i want it to work. The problem is, that
during compiling i obtain the following error.
cannot convert 'void (* (*)[2])(int)' to 'void (***)(int)' for
argument '1' to 'void pm_funcion(void (***)(int), int)'
cannot convert 'void (* (*)[3])(int)' to 'void (***)(int)' for
argument '1' to 'void pm_funcion(void (***)(int), int)'
thanks in advance
klaas
//function
void pm_function(void (***f) (int), int dim) {
for (int i = 0; i < dim; i++)
for (int j = 0; j < dim; j++)
f[i][j](5);
}
int main(void) {
void (*f[2][2])(int);
void (*g[3][3])(int);
You realise of course that you have never initialised these function
pointers to point to anything.
// I want this to work, but it doesn not! See error above message
pm_function(f,2);
pm_function(g,3);
}
Your problem is not specifically with function pointers. You would have the
same problem with arrays of ints, e.g., the following won't compile for the
same reason.
void pm_function(int **pptr, int dim)
{
for (int i = 0; i < dim; i++)
for (int j = 0; j < dim; j++)
cout << pptr[i][j];
}
int main()
{
int arraytwo[2][2];
int arraythree[3][3];
pm_function(arraytwo,2);
pm_function(arraythree,3);
return 0;
}
Array names and pointers are only (more or less) interchangeable when the
array is one dimensional (in the sense of having a single subscript). For
two and more dimensions (in the sense of two or more subscripts) they are
quite different.
To illustrate, if pptr points to an int pointer, then pptr+1 is an address
that is sizeof(int*) greater than pptr. By contrast, using the definition of
arraythree given above, arraythree+1 yields an address that is 3*sizeof(int)
greater than the address of arraythree. The difference is because pptr
points to an int* pointer, whereas arraythree points to a row of three ints.
To prove it, run the following code:
int main()
{
int x;
int *ptr = &x;
int **pptr = &ptr;
cout << "pptr address is " << pptr << '\n';
cout << "pptr+1 address is " << pptr+1 << '\n';
int arraythree[3][3];
cout << "arraythree address is " << arraythree << '\n';
cout << "arraythree+1 address is " << arraythree+1 << '\n';
return 0;
}
If you want to pass 2-dimensional arrays to a function, then you need to a
different function for each different column count. Templates can help here.
--
John Carson