Passing 2D Array to function

I'm trying to pass my 2D array matrix[][] to the function findnorm() but I don't know how to set up the parameters.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

const int ROWCAP = 100; 
const int COLCAP = 100;

int findnorm(int matrix[][], int i, int j);

int main()
{
	int i, j;
	int matrix[ROWCAP][COLCAP];

	for (i = 0; i < ROWCAP; i++)
		for (j = 0; j < COLCAP; j++)
			cin >> matrix[i][j];
}

int findnorm(int matrix[][], int i, int j)
{

}
Last edited on
When passing a multi-dimensional array to a function, only the first dimension may be omitted.
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;

const int ROWCAP = 100; 
const int COLCAP = 100;

int findnorm(int matrix[][COLCAP], int i, int j);

int main()
{
	int i, j;
	int matrix[ROWCAP][COLCAP];

	for (i = 0; i < ROWCAP; i++)
		for (j = 0; j < COLCAP; j++)
			cin >> matrix[i][j];
			
    // following line example of syntax, not actual  code			
    int x = findnorm(matrix, 0, 0);
}

int findnorm(int matrix[][COLCAP], int i, int j)
{

}


See "Arrays as parameters"
http://www.cplusplus.com/doc/tutorial/arrays/
Note: These two declarations are technically identical:
1
2
int findnorm(int matrix[][COLCAP], int i, int j);
int findnorm(int* matrix[COLCAP],  int i, int j);

Seek "array decay to pointer" for more on the subject.
Topic archived. No new replies allowed.