i have bought a book called "c++ for impatient" (released few month's ago i think) and i came across this:
the pointer version is more efficient because it avoids having to evaluate indexes and recalculate the position of elements over and over. It accesses an element directly and than moves to the next position.
with multidimensional arrays, the gain in efficiency is more significant, because calculating the position within such an array is more complex. the pointer version bypasses those calculations.
and then they give me a function example of using 2D array with pointers:
void reset_2D_dbl_array(double **p, int nrows, int ncols){
int n = nrows * ncols;
while(n-- > 0){
**p++ = 0.0;
}
}
so i have been trying to run this code like so:
#include <iostream>
void reset_2D_dbl_array(double **p, int nrows, int ncols);
int main(){
double big_matrix[10][10];
reset_2D_dbl_array(big_matrix,10,10);
std::cout << big_matrix[0][0];
std::cin.ignore();
return 0;
}
void reset_2D_dbl_array(double **p, int nrows, int ncols){
int n = nrows * ncols;
while(n-- > 0){
**p++ = 0.0;
}
}
and the code doesn't seem to be working.. i get an error like this:
1>main.cpp(7): error C2664: 'reset_2D_dbl_array' : cannot convert parameter 1 from 'double [10][10]' to 'double **'
what should i do and how can i make the function run like it should?
*the function just reset all the cells in the array to 0.0*
thanks!