Two Dimensional Array In C

A two dimensional array (2D array) in C is essentially an array of arrays and is often referred to as a matrix. It represents data in rows and columns, making it suitable for applications like storing tabular data or performing matrix operations.

array of arrays in two dimensional array (2D) in c

Initialization of 2D Arrays

There are two main ways to initialize a 2D array during its declaration:

Example 1: Using Nested Braces

int matrix[2][3] = {  {1, 2, 3},          {4, 5, 6}                                  };

Example 2: Using a Single Set of Braces

int matrix[2][3] = {1, 2, 3, 4, 5, 6};

Both are valid, but the first method is more readable, as it clearly shows the rows and columns.

Valid and Invalid Initializing of 2D Arrays

When initializing a 2D array, You must tell the second dimension while declaring a 2D array and can omit the size of the first dimension.

For example:
int matrix[3][2] = {1, 2, 3, 4, 5, 6}; // Valid
int matrix[][2] = {1, 2, 3, 4, 5, 6}; // Valid
int matrix[][] = {1, 2, 3, 4, 5, 6}; // Invalid

Total elements and Size of  two dimensional Array in C

A two-dimensional array (2D array) in C has rows and columns, and the total number of elements in the array can be calculated using the formula:

Number of elements=rows × columns

The array abc[5][4] can hold (5×4), total 20 elements.

Simple Two-Dimensional Array Example

This program shows how to store user-input elements into a 2D array and how to display the array elements.

#include<stdio.h>

int main() {
/* Declare a 2D array */
int matrix[3][2];
/* Counter variables */
int row, col;

/* Input elements into the array */
for(row = 0; row < 3; row++) {
for(col = 0; col < 2; col++) {
printf("Enter value for matrix[%d][%d]: ", row, col);
scanf("%d", &matrix[row][col]);
}
}

/* Display the elements of the array */
printf("Two-Dimensional Array Elements:\n");
for(row = 0; row < 3; row++) {
for(col = 0; col < 2; col++) {
printf("%d ", matrix[row][col]);
}
printf("\n"); // New line for each row
}

return 0;
}

Output:
Enter value for matrix[0][0]: 11
Enter value for matrix[0][1]: 20
Enter value for matrix[1][0]: 13
Enter value for matrix[1][1]: 41
Enter value for matrix[2][0]: 15
Enter value for matrix[2][1]: 60

Two-Dimensional Array Elements:
11 20
13 41
15 60

Important:

  • The outer loop (for( row = 0; row < 3; row++)) iterates through the rows
  • The inner loop (for(col = 0; col < 2; col++)) iterates through the columns

 

Understanding Pointers and 2D Arrays in C

In C, both one-dimensional arrays and two-dimensional arrays have a relationship with pointers. However, the logic behind how these arrays are handled with pointers differs slightly, especially when dealing with 2D arrays.

One-Dimensional Arrays as Pointers:

A one-dimensional array in C acts like a pointer to the first element of the array. For example:

int arr[5] = {1, 2, 3, 4, 5};

Here, arr is equivalent to &arr[0], which is the address of the first element of the array.

Addressing Elements in 2D Arrays:

  • abc[i] in a 2D array gives you the address of the first element of the i-th row.For example,
  • abc[0] holds the address of the first element of the first row (abc[0][0]).
  • abc[1] holds the address of the first element of the second row (abc[1][0]).

Example C Program Demonstrating Pointers with a 2D Array

#include <stdio.h>

int main()
{
int abc[5][4] = {
{0, 1, 2, 3},
{4, 5, 6, 7},
{8, 9, 10, 11},
{12, 13, 14, 15},
{16, 17, 18, 19}
};

// Loop to display the address of the first element of each row
for (int i = 0; i < 5; i++)
{
/* The correct way of displaying an address would be
* printf(“%p “, abc[i]); but for the demonstration
* purpose, we are displaying the address as an integer
* to make it easier to relate the output with the
* memory layout.
*/
printf(“%p “, abc[i]);
}

return 0;
}

Output:

0x7ffeefbff4b0 0x7ffeefbff4d0 0x7ffeefbff4f0 0x7ffeefbff510 0x7ffeefbff530

Note: The addresses shown in the output belong to the first element of each row i.e(abc[0][0]abc[1][0]abc[2][0]abc[3][0] and abc[4][0].).

 

2D Array Traversal in C: Row-Major vs. Column-Major Order

When traversing a 2D array in C, the order in which elements are accessed in memory can be classified into two major types:

  • Row-Major Order
  • Column-Major Order

1. Row-Major Order:

In row-major order, the array elements are stored row by row in memory. That means all elements of the first row are stored first, followed by all elements of the second row.

two dimensional array in c - Row Major Order

Array Name with [Index] Value Memory Address
arr[0][0] 10 2001
arr[0][1] 20 2002
arr[0][2] 30 2003
arr[1][0] 40 2004
arr[1][1] 50 2005
arr[1][2] 60 2006

2. Column-Major Order:

In column-major order, the array elements are stored column by column in memory. That means all elements of the first column are stored first, followed by all elements of the second column, and so on.

two dimensional array in c - Column Major Order

Array Name with [Index] Value Memory Address
arr[0][0] 10 2001
arr[1][0] 40 2002
arr[0][1] 20 2003
arr[1][1] 50 2004
arr[0][2] 30 2005
arr[1][2] 60 2006

Two Dimensional Array: Address Calculation of Any Element  

The 2-dimensional array also called array of arrays. Imagine a 2D array as a grid, similar to a matrix,

where each element has a specific row and column position.

To find the address of an element in a 2D array, there are two main methods:

  • Row Major Order: Elements are stored row-by-row.
  • Column Major Order: Elements are stored column-by-column

2-D array:  Row Major Order

In a row-major order, the elements of an array are stored one row at a time. This means all elements in the first row are stored first in memory, then all elements in the second row, and so on.

To find the address of an element in a 2D array using row-major order, we use this formula:

Address of A[row][col] = Base + Size * ((row – SR) * total_cols + (col – SC))

Here’s what each part means:

  • row: Row number of the element we want to find.
  • col: Column number of the element we want to find.
  • Base: The starting address of the array in memory.
  • Size: The size (in bytes) of each element in the array.
  • SR: The starting row index (assume 0 if not given).
  • SC: The starting column index (assume 0 if not given).
  • total_cols: Total number of columns in the array.

Question 

Given an array, arr[2………12][3………18] with a base address of 200 and each element having a size of 2 bytes in memory, find the address of arr[10][5] using row-major order.

Note: arr[2…12][3…18], means rows ranging from 2 to 12 and columns from 3 to 18.

Solution:

  • Base = 200
  • Size = 2 bytes
  • row = 10
  • col = 5
  • SR = 2
  • SC = 3
  • total_cols= rang [3 to 18] =16

Formula:

Address of A[row][col] = Base + Size * {(row – SR) * total_cols + (col – SC)}

Solution:

Address of A[10][5] = 200 + 2 * {(10 – 2) * 16 + (5 – 3)}
= 200 + 2 * {(8) * 16 + (2)}
= 100 + 2 * {130}
Address of A[10][5] = 460

 

2D array: Column Major Order

In a column-major order, the elements of an array are stored one column at a time. This means all elements in the first column are stored first in memory, then all elements in the second column, and so on.

To find the address of an element in a 2D array using column-major order, we use this formula:

Address of A[row][col] = Base + Size * {(col – SC)* total_rows + (row – SR)}

Here’s what each part means:

  • row: Row number of the element we want to find.
  • col: Column number of the element we want to find.
  • Base: The starting address of the array in memory.
  • Size: The size (in bytes) of each element in the array.
  • SR: The starting row index (assume 0 if not given).
  • SC: The starting column index (assume 0 if not given).
  • total_rows: Total number of rows in the array.

Example:

Given an array, arr[2………12][3………18] with a base address of 200 and each element having a size of 2 bytes in memory, find the address of arr[10][5] using column-major order.

Note: arr[2…12][3…18] means rows ranging from 2 to 12 and columns from 3 to 18.

Given:

  • Base = 200
  • Size = 2 bytes
  • row = 10
  • col = 5
  • SR = 2
  • SC = 3
  • total_rows = range [2 to 12] = 11

Formula:

Address of A[row][col] = Base + Size * {(col – SC)* total_rows + (row – SR)}

 

Solution

Address of A[10][5] = 200 + 2 * {(5 – 3) * 11 + (10 – 2)}
= 200 + 2 * {(2) * 11 + (8)}
= 200 + 2 * {30}
Address of A[10][5] = 260

The address of A[10][5] in column-major order is 260.