Passing a matrix in a function (C)

You need to pass a pointer with as much levels of indirection (*) as the number of dimensions of your matrix.

For example, if your matrix is 2D (e.g. 10 by 100), then:

void ins (int **matrix, int row, int column);

If you have a fixed dimension (e.g. 100), you can also do:

void ins (int (*matrix)[100], int row, int column);

or in your case:

void ins (int (*matrix)[SIZE], int row, int column);

If both your dimensions are fixed:

void ins (int matrix[10][100], int row, int column);

or in your case:

void ins (int matrix[SIZE][SIZE], int row, int column);

If you have a modern C compiler you can do the following for 2D matrices of any sizes

void ins (size_t rows, size_t columns, int matrix[rows][columns]);

Important is that the sizes come before the matrix, such that they are known, there.

Inside your function you then can access the elements easily as matrix[i][j] and the compiler is doing all the index calculations for you.

Tags:

C

Matrix