C two-dimensional array

It is also possible for arrays to have two or more dimensions. The two dimensional array is also called as a matrix. In C language two dimensional array is represented by rows and columns. It is also known as an array of arrays or list of arrays. This two dimensional array can be called as a multi dimensional array.

A pictorial representation of the 2-D array:

The following figure shows how data is stored in memory in matrix form:

two dimensional array

Declaration of two dimensional array:

For example: int arr[4][3];
Here the first dimension always represents a row and the second dimension represents a column. In above example 4 is the row number and 3 is a column number. An arr is an array of data type int.

Initialisation of Two-Dimensional Array:

You can initialise two dimensional array in any of the following two ways:

This example is represented in matrix form as follow:

Points to remember:

  • You can skip row size. But it is mandatory to give column size.

    It is represented as follow:

  • This is represented as follow:

  • It is represented as:

  • Empty brackets are not allowed.

  • Following initilisations are not valid.
    • column size is not given so it is invalid.

    • Variable name is not allowed as row_size or column_size:

    • Since array is are of integer type and contain 12 elements. Therefore (4*3)*4=48 bytes(i.e number of rows * number of columns)

    Accessing 2-D array elements using subscripts-

    The 4th element from 3rd row of the array is assign to var. In this way, we can access any element from array.
    Address of an element can be written as:

    address = *(array_name + rows x sizeof(data_type))+column x sizeof(data_type)
    &a[i][j] = *(a + i) + j
    & arr[0][1] = *((arr + 1 x 4)+1 x 4) = 104

    For example:

    Example of 2-D Array in C


Help others by sharing the content!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.