I started studying C++ at university 2 months ago, and I'm trying to figure out how to pass a dynamic 2d array (pointer to pointer) to a function as an input parameter. I have this dynamic 2d array:
int **p;
p = new int*[R];
for(i=0; i<R; i++)
p[i] = new int[C];
Now I want to pass this pointer p to a function as an input parameter, using const. I mean that I want the function not to be able to modify the elements of the matrix.
I tried like that:
void func(const int **p) {
}
But i get this error:
main.cpp:19:11: error: invalid conversion from 'int**' to 'const int**' [-fpermissive]
main.cpp:9:6: error: initializing argument 1 of 'void func(const int**)' [-fpermissive]
I tryied using typedef and it works, but it's not constant. If i do like that:
typedef int** abc;
void func(const abc p);
main() {
abc p;
...
func(p);
}
The source compiles but function 'func' is able to modify values of "p matrix"; I want p to be an input parameter, it has to be read-only!
Please, how can I pass a pointer-to-pointer to a function flagging the elements as read-only?
Thank you in advance for you help and sorry for my bad english.