Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to multiply two matrices using pointers in C?
Matrix multiplication is a fundamental operation in linear algebra. In C programming, we can multiply two matrices using pointers to access matrix elements efficiently. This approach demonstrates how pointer arithmetic works with 2D arrays.
Syntax
// Matrix multiplication using pointers *(*(result + i) + j) = sum of (*(*(mat1 + i) + k)) * (*(*(mat2 + k) + j))
Understanding Pointer Access in 2D Arrays
When working with 2D arrays and pointers −
-
mat[i][j]is equivalent to*(*(mat + i) + j) -
mat + ipoints to the i-th row -
*(mat + i)gives the address of the first element in i-th row -
*(mat + i) + jpoints to the j-th element in i-th row -
*(*(mat + i) + j)gives the value at mat[i][j]
Example: Matrix Multiplication Using Pointers
Here's a complete program that multiplies two 3x3 matrices using pointer notation −
#include <stdio.h>
#define ROW 3
#define COL 3
/* Function declarations */
void matrixInput(int mat[][COL]);
void matrixPrint(int mat[][COL]);
void matrixMultiply(int mat1[][COL], int mat2[][COL], int res[][COL]);
int main() {
int mat1[ROW][COL] = {{2, 3, 1}, {2, 5, 6}, {2, 6, 8}};
int mat2[ROW][COL] = {{1, 2, 1}, {2, 3, 4}, {5, 6, 7}};
int product[ROW][COL];
printf("First matrix:<br>");
matrixPrint(mat1);
printf("\nSecond matrix:<br>");
matrixPrint(mat2);
matrixMultiply(mat1, mat2, product);
printf("\nProduct of both matrices:<br>");
matrixPrint(product);
return 0;
}
void matrixPrint(int mat[][COL]) {
int row, col;
for (row = 0; row < ROW; row++) {
for (col = 0; col < COL; col++) {
printf("%d ", *(*(mat + row) + col));
}
printf("<br>");
}
}
void matrixMultiply(int mat1[][COL], int mat2[][COL], int res[][COL]) {
int row, col, i;
int sum;
for (row = 0; row < ROW; row++) {
for (col = 0; col < COL; col++) {
sum = 0;
for (i = 0; i < COL; i++) {
sum += (*(*(mat1 + row) + i)) * (*(*(mat2 + i) + col));
}
*(*(res + row) + col) = sum;
}
}
}
First matrix: 2 3 1 2 5 6 2 6 8 Second matrix: 1 2 1 2 3 4 5 6 7 Product of both matrices: 13 19 21 42 55 64 54 70 82
How Matrix Multiplication Works
The multiplication follows this formula for each element −
result[i][j] = sum of (mat1[i][k] * mat2[k][j]) for k = 0 to COL-1
Using pointer notation −
*(*(res + i) + j) = sum of (*(*(mat1 + i) + k)) * (*(*(mat2 + k) + j))
Key Points
- Matrix multiplication requires the number of columns in the first matrix to equal the number of rows in the second matrix
- Pointer arithmetic
*(*(mat + row) + col)is equivalent tomat[row][col] - The nested loops iterate through rows, columns, and perform dot product calculation
- Time complexity is O(n³) for n×n matrices
Conclusion
Matrix multiplication using pointers in C demonstrates advanced pointer arithmetic with 2D arrays. This approach provides direct memory access and helps understand how arrays are stored and accessed in memory.
Advertisements
