# C Program to Perform Matrix Multiplication using Pointers

In this post, we will study how to perform matrix multiplication using pointers.

Before we go any further, it is highly recommended to read Matrix Multiplication.

The program to perform matrix multiplication using pointers is very similar to matrix multiplication without pointers. We just need to replace a few lines in the code.

1. Array[i] is equivalent to *(Array+i).
2. Array[i][j] is equivalent to *( (Array + i) + j ).

## Program of Matrix Multiplication using Pointers

``````#include<stdio.h>

// The function multiply matrixA and matrixB
// and stores the result of multiplication in matrixC
// rA: number of rows in matrixA
// cA: number of columns in matrixA
// rB: number of rows in matrixB
// cB: number of columns in matrixB
void MatrixMultiplication(int matrixA[][50], int rA, int cA, int matrixB[][50], int rB, int cB, int matrixC[][50]) {

int i, j, k, sum = 0;

if (cA != rB) {
printf("Order of Input Matricis is Invalid");
return;
}

for (i = 0; i < rA; ++i) {
for (j = 0; j < cB; ++j) {
sum = 0;
for (k = 0; k < rA; ++k) {
sum += *(*(matrixA + i) + k) * *(*(matrixB + k) + j);
}
*(*(matrixC + i) + j) = sum;
}
}

}

int main() {

int i, j;
int rA, cA, rB, cB;
int matrixA[50][50], matrixB[50][50], matrixC[50][50];

// Enter Matrix A
printf("Enter Number of Rows in MatrixA: ");
scanf("%d", &rA);
printf("Enter Number of columns in MatrixA: ");
scanf("%d", &cA);

printf("Enter MatrixA: \n");
for (i = 0; i < rA; ++i) {
for (j = 0; j < cA; ++j) {
scanf("%d", &matrixA[i][j]);
}
}

// Enter Matrix B
printf("Enter Number of Rows in MatrixB: ");
scanf("%d", &rB);
printf("Enter Number of columns in MatrixB: ");
scanf("%d", &cB);

printf("Enter MatrixB: \n");
for (i = 0; i < rB; ++i) {
for (j = 0; j < cB; ++j) {
scanf("%d", &matrixB[i][j]);
}
}

// the function multiply matrixA and matrixB and stores
// the result in matrixC
// the order of matrixC is rA X cB
MatrixMultiplication(matrixA, rA, cA, matrixB, rB, cB, matrixC);

// Printing MatrixC
printf("\nThe Matrix Multiplication of MatrixA and MatrixB: \n");
for (i = 0; i < rA; ++i) {
for (j = 0; j < cB; ++j) {
printf("%d ", matrixC[i][j]);
}
printf("\n");
}

}``````

## Matrix Multiplication using Pointers ( Recursive )

``````#include<stdio.h>

// The function multiply matrixA and matrixB
// and stores the result of multiplication in matrixC
void MatrixMultiplication(int matrixA[][50], int rA, int cA, int matrixB[][50], int rB, int cB, int matrixC[][50]) {

int i, j, k, sum = 0;

if (cA != rB) {
printf("Order of Input Matricis is Invalid");
return;
}

for (i = 0; i < rA; ++i) {
for (j = 0; j < cB; ++j) {
sum = 0;
for (k = 0; k < rA; ++k) {
sum += *(*(matrixA + i) + k) * *(*(matrixB + k) + j);
}
*(*(matrixC + i) + j) = sum;
}
}

}

int main() {

int i, j;
int rA, cA, rB, cB;
int matrixA[50][50], matrixB[50][50], matrixC[50][50];

// Enter Matrix A
printf("Enter Number of Rows in MatrixA: ");
scanf("%d", &rA);
printf("Enter Number of columns in MatrixA: ");
scanf("%d", &cA);

printf("Enter MatrixA: \n");
for (i = 0; i < rA; ++i) {
for (j = 0; j < cA; ++j) {
scanf("%d", &matrixA[i][j]);
}
}

// Enter Matrix B
printf("Enter Number of Rows in MatrixB: ");
scanf("%d", &rB);
printf("Enter Number of columns in MatrixB: ");
scanf("%d", &cB);

printf("Enter MatrixB: \n");
for (i = 0; i < rB; ++i) {
for (j = 0; j < cB; ++j) {
scanf("%d", &matrixB[i][j]);
}
}

// the function multiply matrixA and matrixB and stores
// the result in matrixC
// the order of matrixC is rA X cB
MatrixMultiplication(matrixA, rA, cA, matrixB, rB, cB, matrixC);

// Printing MatrixC
printf("\nThe Matrix Multiplication of MatrixA and MatrixB: \n");
for (i = 0; i < rA; ++i) {
for (j = 0; j < cB; ++j) {
printf("%d ", matrixC[i][j]);
}
printf("\n");
}

}``````

Output