Back to C Programming
2026-07-125 min read

Multiply two Matrices by Passing Matrix to a Function

Learn Multiply two Matrices by Passing Matrix to a Function step by step with clear examples and exercises.

Why This Matters

Matrix multiplication is a fundamental operation in linear algebra with numerous applications, such as image processing, computer graphics, and machine learning. In this guide, we'll learn how to implement matrix multiplication using C functions, which will help you solve complex problems efficiently.

The Importance of Matrix Multiplication

Matrix multiplication is a powerful tool for solving linear equations, transforming coordinates, and analyzing data in various fields like physics, engineering, computer science, and more. Understanding how to multiply matrices using C functions can enable you to tackle complex problems and improve your problem-solving skills.

Prerequisites

To follow this guide, you should have a good understanding of the following concepts:

  • Basic C programming concepts, including variables, loops, and functions
  • Data structures in C, particularly arrays
  • Understanding of pointers in C
  • Linear algebra basics (optional but recommended)

Core Concept

Defining Matrices as Arrays

In C, we can represent matrices using 2D arrays. A matrix with m rows and n columns can be represented as an array of m * n elements. For instance:

int matrix1[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};

Here, matrix1 is a 3x4 matrix. We can access its elements using the following syntax:

  • matrix1[i][j] to get the element at row i and column j.
  • matrix1[i] to get the entire row i.
  • &matrix1[i] to get the address of the entire row i.

Writing a Function for Matrix Multiplication

To multiply two matrices, we'll write a function that takes two matrices as arguments and returns their product. Here's a simple implementation:

#include <stdio.h>

void multiplyMatrices(int **matrix1, int rows1, int cols1, int **matrix2, int rows2, int cols2, int **result) {
for (int i = 0; i < rows1; ++i) {
for (int j = 0; j < cols2; ++j) {
result[i][j] = 0;
for (int k = 0; k < cols1; ++k) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
}

In this function, matrix1, matrix2, and result are pointers to the matrices we want to multiply. The first two arguments define the dimensions of the input matrices, while the last argument is a pointer to the output matrix that will store the product.

Calling the Function

To call the function, we'll create two matrices and allocate memory for their product:

int main() {
int rows1 = 3;
int cols1 = 4;
int rows2 = 4;
int cols2 = 3;

int **matrix1 = (int**) malloc(rows1 * sizeof(int*));
for (int i = 0; i < rows1; ++i) {
matrix1[i] = (int*) malloc(cols1 * sizeof(int));
}

int **matrix2 = (int**) malloc(rows2 * sizeof(int*));
for (int i = 0; i < rows2; ++i) {
matrix2[i] = (int*) malloc(cols2 * sizeof(int));
}

int **result = (int**) malloc((rows1 > rows2 ? rows1 : rows2) * sizeof(int*));
for (int i = 0; i < (rows1 > rows2 ? rows1 : rows2); ++i) {
result[i] = (int*) malloc(cols1 > cols2 ? cols1 : cols2 * sizeof(int));
}

// Fill the matrices with data...

multiplyMatrices(matrix1, rows1, cols1, matrix2, rows2, cols2, result);

// Print the product...

return 0;
}

In this example, we first allocate memory for our matrices and their product. We then fill the matrices with data (not shown here), call the multiplyMatrices() function to compute the product, and finally print the result.

Worked Example

Let's take two 3x4 and 4x3 matrices as examples:

int rows1 = 3;
int cols1 = 4;
int rows2 = 4;
int cols2 = 3;

int **matrix1 = ...; // Fill with data...
int **matrix2 = ...; // Fill with data...

If we define the matrices as follows:

// Matrix 1:
int matrix1[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};

// Matrix 2:
int matrix2[4][3] = {
{13, 14, 15},
{16, 17, 18},
{19, 20, 21},
{22, 23, 24}
};

The product of these matrices can be computed as:

int **result = (int**) malloc(3 * sizeof(int*));
for (int i = 0; i < 3; ++i) {
result[i] = (int*) malloc(3 * sizeof(int));
}

multiplyMatrices(matrix1, rows1, cols1, matrix2, rows2, cols2, result);

The resulting product will be:

// Product:
int result[3][3] = {
{65, 78, 91},
{154, 182, 210},
{243, 286, 330}
};

Common Mistakes

  • Forgetting to allocate memory for the output matrix: Make sure you allocate enough memory for the product of the two matrices.
  • Incorrectly accessing matrix elements: Be careful when accessing matrix elements to avoid index out-of-bounds errors.
  • Not handling cases where the number of columns in the first matrix is not equal to the number of rows in the second matrix: If the matrices cannot be multiplied, the function should return an error or a null pointer.

Common Mistakes (Continued)

  • Failing to check for compatible dimensions: Always verify that the number of columns in the first matrix matches the number of rows in the second matrix before attempting to multiply them.
  • Not properly handling memory allocation errors: If memory allocation fails, handle the error gracefully and exit the program or return an error code.
  • Neglecting to free allocated memory: After using a matrix, remember to free its memory to avoid memory leaks.

Practice Questions

  1. Write a C program to find the product of two 4x4 matrices.
  2. Modify the multiplyMatrices() function to handle transposed matrices.
  3. Implement a function to add two matrices in C.
  4. Write a C program that finds the determinant of a 2x2 matrix using C.
  5. Extend the multiplyMatrices() function to support matrices with different data types (e.g., float or double).

FAQ

Q: How can I check if two matrices can be multiplied?

A: You can check if two matrices can be multiplied by verifying that the number of columns in the first matrix is equal to the number of rows in the second matrix. If this condition is not met, the matrices cannot be multiplied directly.

Q: What happens when I multiply a 3x4 matrix with a 4x2 matrix?

A: In this case, the matrices cannot be multiplied directly because the number of columns in the first matrix (4) is not equal to the number of rows in the second matrix (2). If you still want to perform the operation, you can transpose one or both matrices so that they have compatible dimensions.

Q: How can I find the determinant of a 2x2 matrix using C?

A: To find the determinant of a 2x2 matrix in C, use the following formula: determinant = matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0].

Q: How can I transpose a matrix using C?

A: To transpose a matrix in C, you can swap the rows and columns of the original matrix. Here's an example function that transposes a 2D array:

void transposeMatrix(int **matrix, int rows, int cols) {
for (int i = 0; i < rows; ++i) {
for (int j = i + 1; j < cols; ++j) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
}

This function takes a 2D array and its dimensions as arguments, swaps the rows and columns to transpose the matrix in-place.