passing a 2D array in a function

I am a beginner of C++. Any help will be very appreciated!

I can successfully compile and run the following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
using namespace std;
void printMatrix(int matrix[4][4]){
     for(int i=0;i<4;i++)
    {
         for(int j=0;j<4;j++)
         {
              cout<<matrix[i][j]<<" ";   
         }   
         cout<<endl;
    }
     
}
int main(){
    int matrix[4][4] = {{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4}};
    printMatrix(matrix);
    system("pause");
    return 0;
    }


However, what i want to do is like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  void printMatrix(int **matrix, int r, int c){
     for(int i=0;i<r;i++)
    {
         for(int j=0;j<c;j++)
         {
              cout<<matrix[i][j]<<" ";   
         }   
         cout<<endl;
    }   
}
int main(){
    int matrix[4][4] = {{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4}};
    printMatrix(matrix);
    system("pause");
    return 0;
    }


Every time when i compiled, it had an error:
cannot convert `int (*)[4]' to `int**' for argument `1' to `void printMatrix(int**, int, int)'

Any ideas? Thank you!
Last edited on
in printMatrix, matrix is a pointer to a pointer to data. The function has no idea how many pointers matrix is pointing to. You need to do define one of the dimensions, leaving only one to be interpreted by the compiler. Otherwise there isn't enough information for the function to find the specific value. Do it like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void printMatrix(int matrix[4][], int r, int c){
     for(int i=0;i<r;i++)
    {
         for(int j=0;j<c;j++)
         {
              cout<<matrix[i][j]<<" ";   
         }   
         cout<<endl;
    }   
}
int main(){
    int matrix[4][4] = {{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4}};
    printMatrix(matrix);
    system("pause");
    return 0;
}


Edit: If you don't know the size of either dimension of the matrix at compile time, you may just want to use a 1d array. This is much easier to work with for both the compiler and you. It would be done like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void printMatrix(int matrix[], int r, int c){
     for(int i=0;i<r;i++)
    {
         for(int j=0;j<c;j++)
         {
              cout<<matrix[i*c+j]<<" ";   
         }   
         cout<<endl;
    }   
}
int main(){
    int matrix[16] = {1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4};
    printMatrix(matrix, 4, 4);
    system("pause");
    return 0;
}
Last edited on
Another option is to create global constants to hold the "MAX_SIZE" of your variables. This way you can quickly change the sizes and then use the variable MAX_SIZE everywhere else in your program like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const int MAX_ROWS = 4;
const int MAX_COLS = 4;

void printMatrix(int matrix[MAX_ROWS][MAX_COLS], int r, int c){
     for(int i=0;i<r;i++)
    {
         for(int j=0;j<c;j++)
         {
              cout<<matrix[i][j]<<" ";   
         }   
         cout<<endl;
    }   
}
int main(){
    int matrix[MAX_ROWS][MAX_COLS] = {{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4}};
    printMatrix(matrix, 4, 4);
    system("pause");
    return 0;
}
Last edited on
@Stewbond

void printMatrix(int matrix[4][], int r, int c);


There is an invalid declaration of the first parameter.

@wnan42

You can define the function such a way as below only if you are sure that your two dimensional array has exactly 4 rows.

1
2
3
4
5
6
7
8
9
10
11
void printMatrix(int matrix[4][4]){
     for(int i=0;i<4;i++)
    {
         for(int j=0;j<4;j++)
         {
              cout<<matrix[i][j]<<" ";   
         }   
         cout<<endl;
    }
     
}


The more general approach is to specify number of rows as a parameter of the function

1
2
3
4
5
6
7
8
9
10
11
void printMatrix(int matrix[4][4], int rows ){
     for(int i=0;i<rows;i++)
    {
         for(int j=0;j<4;j++)
         {
              cout<<matrix[i][j]<<" ";   
         }   
         cout<<endl;
    }
     
}


The problem as the declaration of your function

void printMatrix(int matrix[4][4], int rows );

is equivalent to the following declarations

void printMatrix(int matrix[10][4], int rows )

and

void printMatrix(int *matrix[[4], int rows )

Parameter of type of some (one dimensional or n-dimensional) array is implicitly converted to the poimter to its first element. For two-dimensional array iits element is a one-dimensional array.

If you are going to use the following declaration of your function

void printMatrix(int **matrix, int r, int c);

then in the main you shall call it as

printMatrix( reinterpret_cast<int **>( matrix ), 4, 4 );





It wasn't THAT invalid.

void printMatrix(int matrix[][4], int r, int c){

was the correct way to do it. We couldn't just arbitrarily choose which one we were going to define, it had to be the last one.
I suspect that what you are wanting to do is write a function that will print a matrix of arbitrary size, and if so you are going to be bitterly disappointed. This code will do that:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

using namespace std;

void printMatrix(int *matrix, int r, int c)
{
    for (int i=0;i<r;i++)
    {
        for (int j=0;j<c;j++)
        {
            cout<<matrix[i*c+j]<<" ";
        }
        cout<<endl;
    }
}

int main()
{
    int matrix[4][4] = {{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4}};
    int m1[5][3] = {{1,2,3},{4,5,6},{7,8,9},{10,11,12},{13,14,15}};
    printMatrix((int *)matrix, 4, 4);
    printMatrix((int *)m1,5,3);
    system("pause");
    return 0;
}

What this printMatrix does is to treat the matrix as if it were a 1D array and calculate an index into that using the row and column and number of columns (as [i*c+j]). Note that the matrix parameter is declared as int * not int **. Handling of multidimensional arrays of variable size is one of the ugliest aspects of C/C++, and makes you wish you had used Java or Lisp or just about anything else.
All the previous suggestions hard-code the number of rows or columns into printMatrix. In general, if you are going to pass multidimensional arrays as parameters to a function, you have to hard-code the sizes of all dimensions, as constants, apart from the first one. If that isn't good enough, you have to treat it as a 1D array and calculate the indexing yourself. To do that, you have to understand the way multidimensional arrays are stored in memory.
Last edited on

@Stewbond

It wasn't THAT invalid.

void printMatrix(int matrix[][4], int r, int c){

was the correct way to do it. We couldn't just arbitrarily choose which one we were going to define, it had to be the last one.


Do you see the difference between the first your declaration

void printMatrix(int matrix[4][], int r, int c);

and the second your declaration

void printMatrix(int matrix[][4], int r, int c);

of the function?
Topic archived. No new replies allowed.