Passing a matrix in a function C

To pass a matrix in a function in C, you can use a pointer to an array. Here's an example:

#include <stdio.h>

void printMatrix(int (*matrix)[3], int rows, int cols) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
}

int main() {
    int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
    printMatrix(matrix, 2, 3);
    return 0;
}

In this example, the printMatrix function takes a pointer to a 2D array and the number of rows and columns as parameters. Inside the function, we can access the elements of the matrix using the pointer notation matrix[i][j]. The function simply prints the elements of the matrix.

In the main function, we declare a 2D array matrix and initialize it with some values. We then call the printMatrix function, passing the matrix array, the number of rows (2), and the number of columns (3) as arguments.

When you run this program, it will print the elements of the matrix:

1 2 3
4 5 6

This demonstrates how to pass a matrix as a parameter to a function in C.