Skip to main content
GameDev.net gamedev.net
🔒 Locked

2D array and pointers

Started by MzDay Aug 6, 2013 at 1:57 PM 3 replies 1.7k views
Original Post
MzDay
MzDay

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!

MzDay
MzDay

This should hopefully clarify things a bit :-)

i didnt really understand much lol

um..

if i make an array like this:
int darr[10][10];

and pass it to a function, the function get an address to an array of 8 int's is that right?

meaning that i will get the start address of the whole array ?

i really dont get it S:

can you please explain it to me with my function? :\

thanks!

MzDay
MzDay

UPDATE:

this seem to fixed it, im not sure way.. can you explain please?:


#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[0],10,10);
    std::cout << big_matrix[9][2];
    std::cin.ignore();
    return 0;
}
void reset_2D_dbl_array(double *p, int nrows, int ncols){
  int n = nrows * ncols;
  while(n-- > 0){
    *p++ = 1.0;
  }
} 
myers80
myers80

When arrays are passed to functions they decay to pointers to their first element. big_matrix is an array of 10 arrays of 10 doubles, so when it's passed to reset_2D_dbl_array it decays to a pointer to an array of 10 doubles (i.e. double [10][10] decays to double(*)[10], not double**).

In your second example, big_matrix[0] is an array of 10 doubles, so it would decay to a pointer to a double (i.e. double[10] decays to double*).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.