A 2D array, also known as a two-dimensional array, is a data structure in programming that stores values in a grid or matrix format with rows and columns. It is essentially an array of arrays, where each element of the outer array is itself an array that holds a fixed number of elements of the same data type. A 2D array is useful for storing and manipulating data that is arranged in a tabular format, such as a spreadsheet or a chessboard. Accessing elements in a 2D array requires specifying both the row and column index of the element.

- A sort of array known as a 2D array is capable of storing data in a structure resembling a table with rows and columns.
- Because it represents a mathematical value matrix, it is also known as a matrix.
- Two sets of square brackets, one for the rows and one for the columns, are used to declare 2D arrays.
- A 2D array has two indices: one for the row and one for the column, which may be used to retrieve its members.
- Data that is organised in a grid or table manner, such as photos, maps, and spreadsheets, can be stored in 2D arrays.
- They have a wide range of uses, including scientific calculations, image processing, and visual programming.
- Game boards and other visual elements can be represented using 2D arrays.
Syntax of 2D Arrays in C Programming
data_type array_name[row_size][column_size]; int myArray[3][4];
Here, data_type
specifies the type of data that the array will hold, array_name
is the name given to the array, row_size
specifies the number of rows in the 2D array, and column_size
specifies the number of columns in the 2D array.
For example, to declare a 2D integer array with 3 rows and 4 columns:
This creates a 2D array called myArray
that can hold 3 rows and 4 columns of integers.
Example of 2D Arrays in C Programming
#include <stdio.h> #define ROWS 3 #define COLS 4 int main() { int arr[ROWS][COLS]; // Input values printf("Enter %d values for a %d x %d array:\n", ROWS*COLS, ROWS, COLS); for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { scanf("%d", &arr[i][j]); } }#include <stdio.h> int main() { int rows, columns; printf("Enter the number of rows: "); scanf("%d", &rows); printf("Enter the number of columns: "); scanf("%d", &columns); int arr[rows][columns]; printf("Enter the elements of the array:\n"); // nested loop for input for(int i = 0; i < rows; i++) { for(int j = 0; j < columns; j++) { printf("arr[%d][%d]: ", i, j); scanf("%d", &arr[i][j]); } } printf("The elements of the array are:\n"); // nested loop for output for(int i = 0; i < rows; i++) { for(int j = 0; j < columns; j++) { printf("%d ", arr[i][j]); } printf("\n"); } return 0; } // Output values printf("\nThe values you entered are:\n"); for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { printf("%d ", arr[i][j]); } printf("\n"); } return 0; }
This program prompts the user to input the number of rows and columns for a 2D array. Then, it creates the array, receives user input using nested loops, and finally displays the array elements using another set of nested loops.
Output
Enter the number of rows: 3
Enter the number of columns: 2
Enter the elements of the array:
arr[0][0]: 1
arr[0][1]: 2
arr[1][0]: 3
arr[1][1]: 4
arr[2][0]: 5
arr[2][1]: 6
The elements of the array are:
1 2
3 4
5 6
Frequently Asked Questions
A: A 2D array is an array of arrays. It is a collection of elements of the same data type that are arranged in rows and columns.
A: To declare a 2D array in C, you need to specify the data type of the elements in the array, the name of the array, and the number of rows and columns in the array. For example, the following code declares a 2D array of 3 rows and 4 columns:
int myArray[3][4];
A: You can access elements in a 2D array by using the row and column indices. For example, to access the element in the second row and third column of the array declared above, you would use the following code:
int element = myArray[1][2];
A: No, the size of an array cannot be changed at runtime in C. You need to declare the size of the array before compiling the program.
To initialize a 2D array in C, you can use the following syntax:
datatype arrayname[rows][columns] = {{value1, value2, …}, {value1, value2, …}, …};
int myArray[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
A: An array is a collection of elements of the same data type that are stored in contiguous memory locations. Each element in the array can be accessed by its index, which starts from 0.
A: To declare an array in C, you need to specify the data type of the elements in the array, the name of the array, and the number of elements in the array. For example, the following code declares an array of 10 integers.
A: You can initialize an array in C by assigning values to the elements in the array at the time of declaration. For example, the following code declares and initializes an array of 5 integers:
int myArray[5] = {1, 2, 3, 4, 5};
A: No, the size of an array cannot be changed at runtime in C. You need to declare the size of the array before compiling the program.
A: You can access elements in an array by using the array index. For example, to access the first element in the array declared above, you would use the following code:
int firstElement = myArray[0];