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.
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:
Understanding Pointers and 2D Arrays in CIn 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, Addressing Elements in 2D Arrays:
Example C Program Demonstrating Pointers with a 2D Array#include <stdio.h> int main() // Loop to display the address of the first element of each row return 0; Output: 0x7ffeefbff4b0 0x7ffeefbff4d0 0x7ffeefbff4f0 0x7ffeefbff510 0x7ffeefbff530
2D Array Traversal in C: Row-Major vs. Column-Major OrderWhen traversing a 2D array in C, the order in which elements are accessed in memory can be classified into two major types:
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.
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: Address Calculation of Any ElementThe 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:
2-D array: Row Major OrderIn 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:
QuestionGiven 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:
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)}
2D array: Column Major OrderIn 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:
Example:
|