C program to add two matrix using pointers

Write a C program to add two matrix using pointers. C program to input two matrix from user and find sum of both matrices using pointers.

Example

Input

Input matrix1: 
1 2 3
4 5 6
7 8 9
Input matrix2: 
9 8 7
6 5 4
3 2 1

Output

Sum of both matrices:
10 10 10
10 10 10
10 10 10

Required knowledge

Functions, Multi dimensional Array, Pointers, Pointers and Arrays

Matrix addition

Sum of two matrix A and B of size mXn is defined by
(A + B) = Aij + Bij (where 1 ≤ i ≤ m and 1 ≤ j ≤ n).

Matrix addition

How to add two matrices using pointers

In my previous posts, I have already explained how easily you can add two matrices without using pointers. There is not much changes in program except for pointer notation instead of array notation.

To add two matrices in array notation we use
res[i][j] = mat1[i][j] + mat2[i][j] (where res is resultant array to store sum of mat1 and mat2).

Now, instead of using array notation we can use pointer notation. In pointer notation sum of two matrices is written as,
*(*(res + i) + j) = *(*(mat1 + i) + j) + *(*(mat2 + i) + j)

Note: If you are facing difficulties with the pointer notation. Please give a quick view to access two dimensional array using pointer.

Let us apply the above notation with loops and code it in C program.

Program to add two matrix using pointers

/**
 * C proogram to add two matrix using pointers.
 */

#include <stdio.h>

#define ROWS 3
#define COLS 3


/* Function declaration to input, add and print matrix */
void matrixInput(int mat[][COLS]);
void matrixPrint(int mat[][COLS]);
void matrixAdd(int mat1[][COLS], int mat2[][COLS], int res[][COLS]);


int main()
{
    int mat1[ROWS][COLS], mat2[ROWS][COLS], res[ROWS][COLS];

    // Input elements in first matrix
    printf("Enter elements in first matrix of size %dx%d: \n", ROWS, COLS);
    matrixInput(mat1);

    // Input element in second matrix
    printf("\nEnter elemetns in second matrix of size %dx%d: \n", ROWS, COLS);
    matrixInput(mat2);

    // Finc sum of both matrices and print result
    matrixAdd(mat1, mat2, res);

    printf("\nSum of first and second matrix: \n");
    matrixPrint(res);
    

    return 0;
}



/**
 * Function to read input from user and store in matrix.
 * 
 * @mat     Two dimensional integer array to store input.
 */
void matrixInput(int mat[][COLS])
{
    int i, j;


    for (i = 0; i < ROWS; i++)
    {
        for (j = 0; j < COLS; j++)
        {
            // (*(mat + i) + j) is equal to &mat[i][j]
            scanf("%d", (*(mat + i) + j));
        }
    }
}



/**
 * Function to print elements of matrix on console.
 * 
 * @mat     Two dimensional integer array to print.
 */
void matrixPrint(int mat[][COLS])
{
    int i, j;

    for (i = 0; i < ROWS; i++)
    {
        for (j = 0; j < COLS; j++)
        {
            // *(*(mat + i) + j) is equal to mat[i][j]
            printf("%d ", *(*(mat + i) + j));
        }
        printf("\n");
    }
}



/**
 * Function to add two matrices and store their result in given res
 * matrix.
 * 
 * @mat1    First matrix to add.
 * @mat2    Second matrix to add.
 * @res     Resultant matrix to store sum of mat1 and mat2.
 */
void matrixAdd(int mat1[][COLS], int mat2[][COLS], int res[][COLS])
{
    int i, j;


    // Iterate over each matrix elements 
    for (i = 0; i < ROWS; i++)
    {
        for (j = 0; j < COLS; j++)
        {
            // res[i][j] = mat1[i][j] + mat2[i][j]
            *(*(res + i) + j) = *(*(mat1 + i) + j) + *(*(mat2 + i) + j);
        }
    }
}

Output

Enter elements in first matrix of size 3x3:
1 2 3
4 5 6
7 8 9

Enter elemetns in second matrix of size 3x3:
9 8 7
6 5 4
3 2 1

Sum of first and second matrix:
10 10 10
10 10 10
10 10 10

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>