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 :)

2
  • 1
    You cannot be impatient and a programmer. Commented Aug 6, 2013 at 13:40
  • 1
    LOL, that's what the book is called :P im not impatient :P Commented Aug 6, 2013 at 13:41

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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 In C++, the language that the question was tagged as, memory is an archipelago of memory location clusters.
@R.MartinhoFernandes what do you mean ? A lot of jumps in memory ? a 2D memory ?
@user2485710 A group of isolated clusters of memory locations. C++ does not view memory as "one big array".
@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.
|
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];

2 Comments

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..
(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..
0

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

Comments

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

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.