3

I am trying to get this code working:

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

I don't understand why isn't it working.

I took the code from the new book "C++ for the impatient" and it still doesn't work..

I want to use two "at" signs in the function when changing the value's without index's and square brackets.

EDIT: please look at the second comment of mine for more information, thanks :)

EDIT2: pasted the wrong code :)

Naor Hadar
  • 537
  • 1
  • 6
  • 19

5 Answers5

4

Because double pointers do not decay to double arrays. Just pass a double array to your function:

void reset_double_array(double p[][10], int nrows, int ncols);

For an explanation of why this is so, please see this question and its answers

Seeing you've tagged your question C++, you should really be using std::vector<std::vector<double>> instead to represent your matrix, and save yourself the hassle of pointers and raw arrays.

Community
  • 1
  • 1
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
  • 2
    also a "matrix" or a "double array" is an abstraction for the user, there is no such thing in memory or in C as a "double array", everything in memory is just in 1 dimension. – user2485710 Aug 06 '13 at 12:43
  • 3
    @user2485710 In C++, the language that the question was tagged as, memory is an archipelago of memory location clusters. – R. Martinho Fernandes Aug 06 '13 at 12:48
  • @R.MartinhoFernandes what do you mean ? A lot of jumps in memory ? a 2D memory ? – user2485710 Aug 06 '13 at 12:50
  • 1
    @user2485710 A group of isolated clusters of memory locations. C++ does not view memory as "one big array". – R. Martinho Fernandes Aug 06 '13 at 12:51
  • @R.MartinhoFernandes where do I say that ... also I don't think that POD arrays are a C++ data structure from C++ world, more like a C data structure kept for compatibility with an older language. – user2485710 Aug 06 '13 at 12:56
  • Please see the comment below with my comments added :) thanks :) – Naor Hadar Aug 06 '13 at 13:17
2

You need to pass it as double array

void reset_double_array(double p[][10], int nrows, int ncols){
   for(int i=0;i<nrows;i++)
     for(int j=0;j<ncols;j++)
        p[i][j]=1.0;
}

And fix:

std::cout << big_matrix[9][9]; //instead of std::cout << big_matrix[9];
P0W
  • 46,614
  • 9
  • 72
  • 119
  • That's the problem, in the book im learning from (new one, teaching c++ 11 too), they aren't using any square brackets, just the "at" sign as i did.. – Naor Hadar Aug 06 '13 at 12:56
  • (didnt mean to press enter lol) they are passing it like a regular variable and then use it like pointer.. about the cout i forgot that i fixed and i got you the wrong code sorry lol.. – Naor Hadar Aug 06 '13 at 12:58
0

std::cout << big_matrix[9]; sends an array of doubles to stdout, so it will print the array address (a pointer number)

Remco Boom
  • 131
  • 2
0

It's not quite clear what you are trying to do here. It looks as if you are attempting to set the first double in each row. In which case the following should work:

void reset_double_array(double **p, int nrows, int ncols)
{
    double* val = *p;
    while(nrows-- > 0)
    {
        *val = 1.0;
        val += ncols;
    }
}
dunc123
  • 2,595
  • 1
  • 11
  • 11
0

You can do that using pointers as follows:

void reset_2D_dbl_array(double *p, int nrows, int ncols) { .... }

Then when you are passing the actual 2d array:

reset_2D_dbl_array(&big_matrix[0][0],10,10);

or

reset_2D_dbl_array(big_matrix[0],10,10);

user1044328
  • 85
  • 1
  • 7