How to access two dimensional array using pointers in C programming?

How to access two dimensional array using pointers in C programming? Write a C program to input and print elements of a two dimensional array using pointers and functions.

Example

Input

Input elements in 3x3 matrix: 
1 2 3
4 5 6
7 8 9

Output

Elements of 3x3 matrix: 
1 2 3
4 5 6
7 8 9

Required knowledge

Multi-dimensional array, Pointers, Pointers and Arrays, Functions

How to access two dimensional array using pointers?

To access a two dimensional array using pointer, let us recall basics from one dimensional array. Since it is just an array of one dimensional array.

Suppose I have a pointer array_ptr pointing at base address of one dimensional array. To access nth element of array using pointer we use *(array_ptr + n) (where array_ptr points to 0th element of array, n is the nth element to access and nth element starts from 0).

Now we know two dimensional array is array of one dimensional array. Hence let us see how to access a two dimensional array through pointer.

Let us suppose a two-dimensional array

int matrix[3][3];

For the above array,

matrix               =>    Points to base address of two-dimensional array.
                           Since array decays to pointer.

*(matrix)            =>    Points to first row of two-dimensional array.
*(matrix + 0)        =>    Points to first row of two-dimensional array.
*(matrix + 1)        =>    Points to second row of two-dimensional array.

**matrix             =>    Points to matrix[0][0]
*(*(matrix + 0))     =>    Points to matrix[0][0]
*(*(matrix + 0) + 0) =>    Points to matrix[0][0]
*(*matrix + 1)       =>    Points to matrix[0][1]
*(*(matrix + 0) + 1) =>    Points to matrix[0][1]
*(*(matrix + 2) + 2) =>    Points to matrix[2][2]

Note: If you are not familiar with *(matrix + 2) syntax then please give yourself time and learn pointer arithmetic in C.

I have tried to summarize two dimensional array access using pointer in below image.

Two dimensional array access using pointer
Two dimensional array access using pointer

Program to access a two dimensional array using pointer

/**
 * C program to access two dimensional array using pointer.
 */

#include <stdio.h>

#define ROWS 3
#define COLS 3

/* Function declaration to input and print two dimensional array */
void inputMatrix(int matrix[][COLS], int rows, int cols);
void printMatrix(int matrix[][COLS], int rows, int cols);



int main()
{
    int matrix[ROWS][COLS];
    int i, j;

    /* Input elements in matrix */
    printf("Enter elements in %dx%d matrix.\n", ROWS, COLS);
    inputMatrix(matrix, ROWS, COLS);


    /* Print elements in matrix */
    printf("Elements of %dx%d matrix.\n", ROWS, COLS);
    printMatrix(matrix, ROWS, COLS);


    return 0;
}




/**
 * Function to take input in two dimensional array (matrix) 
 * from user.
 *
 * @matrix  2D array to store input.
 * @rows    Total rows in 2D matrix.
 * @cols    Total columns in 2D matrix.
 */
void inputMatrix(int matrix[][COLS], int rows, int cols)
{
    int i, j;


    for(i = 0; i < rows; i++)
    {
        for(j = 0; j < cols; j++)
        {
            // (*(matrix + i) + j is equivalent to &matrix[i][j]
            scanf("%d", (*(matrix + i) + j));
        }
    }
}



/**
 * Function to display elements of two dimensional array (matrix)
 * on console.
 *
 * @matrix  2D array to display as output.
 * @rows    Total rows in 2D matrix.
 * @cols    Total columns in 2D matrix.
 */
void printMatrix(int (*matrix)[COLS], int rows, int cols)
{
    int i, j;


    for (i = 0; i < rows; i++)
    {
        for (j = 0; j < cols; j++)
        {
            // *(*(matrix + i) + j) is equivalent to matrix[i][j]
            printf("%d ", *(*(matrix + i) + j));
        }

        printf("\n");
    }
}

Note: You can use any of the two notations int matrix[][COLS] or int (*matrix)[COLS], to access two dimensional array using pointers. The first int matrix[][COLS] is general array notation. Whereas int (*matrix)[COLS] is a pointer to array.

Output

Enter elements in 3x3 matrix.
1 2 3
4 5 6
7 8 9
Elements of 3x3 matrix.
1 2 3
4 5 6
7 8 9