Jump to content
  • Advertisement
Sign in to follow this  
MzDay

2D array and pointers

This topic is 1834 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
Advertisement

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!

Share this post


Link to post
Share on other sites

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;
  }
} 

Share this post


Link to post
Share on other sites

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*).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!