1
void print_matrix(double ** m, int rows, int cols)
{
    for(int i = 0; i < rows; ++i)
    {
    for(int j = 0; j < cols; ++j)
    {
        cout << m[i][j] << '\t';
    }
    cout << endl;
    }
}

int main()
{
    double m[4][5] = 
    {
    {2, 3, 4, 5, 6},
    {1, 0, 0, 6, 0},
    {0, 0, 2, 9, -6},
    {9, 8, 7, 6, 5},
    };
    print_matrix(m, 4, 5);
    return 0;
}

main.cpp:30:25: error: cannot convert 'double (*)[5]' to 'double**' for argument '1' to 'void print_matrix(double**, int, int)'
     print_matrix(m, 4, 5);
                         ^

So I think I understand what is wrong here however I would like to know how you would handle this. Do I need to use dynamic memory?

chasep255
  • 10,034
  • 8
  • 45
  • 98

2 Answers2

1

The correct prototype is:

void print_matrix(double (&m)[4][5]);

or even better with const:

void print_matrix(const double (&m)[4][5]);

Live demo: https://ideone.com/ycFZSt

Jarod42
  • 173,454
  • 13
  • 146
  • 250
0

Yes, you have to use dynamic memory instead of static declaration.

An array of arrays does not convert to a pointer to pointers.

Regards

Pd: I assumed that you don't want to use a fixed size array in your function prototype