Two Dimensional Array in C - Syntax, Declaration & Examples (2024)

Last Updated on December 28, 2023 by Ankit Kochar

Two Dimensional Array in C - Syntax, Declaration & Examples (1)

Two-dimensional arrays are fundamental data structures in programming languages like C, allowing the storage and manipulation of data in a grid-like format. They are an arrangement of elements in rows and columns, forming a matrix structure that enables efficient organization and access to data. In C, two-dimensional arrays are extensively used to work with matrices, images, tables, and more, providing a versatile way to handle data in a structured manner. This article delves into the fundamentals of two-dimensional arrays in C, explaining their syntax, usage, and common operations

What is a 2D Array?

A two dimensional array in c is one type of array. The 2d array is an array that is organized in rows and columns. We can identify each element in a 2d array using the position of rows and columns, we also call them indices. For example, in a 3×3 2D array, the element in the second row and third column would be accessed using the indices (1, 2) or row 1, column 2.

Syntax of 2D Array

In this blog section, we will explain the syntax of the two dimensional array in c.

data_type array_name[m][n];

Where,

  • data_type: It will tell the type of data that has to be stored in each element.
  • array_name: It will tell the name of the array with which it will be referenced in the whole program.
  • m: It is the number of rows.
  • n: It is the number of columns.

The array will have a total of m x n elements.
For example, the 2D array in c of 20 rows and 10 columns will be declared like this.

int x[20][10];

Declaration and Initialization of Two Dimensional Array in C

We can declare the 2D array in c by using two methods.

  • Using Initializer List
  • Using Loops

Initializing using Initializer List
In the initializer list we have two methods to declare the two dimensional array in C.

Initializing Two Dimensional Array in C using Loops
We can declare the array as normal after that we can initialize them with the help of loops.

Example of two-dimensional array in C
Let’s understand this better with the help of an example.

int a[m][n];for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++){ x[i][j] = i * j + 1; }}

In the above example we have declared the array with of size m x n and we have initialized it by using loops.

Accessing the Elements of the 2D Array in C

You can access the elements of Two dimensional array in C using the rows and columns or indices shown below.

array_name[x][y]

Where

  • x: is the row index.
  • y: is the column index

Pointers and 2D Array in C

We can use the pointers for accessing the elements of the array. The single pointer points to an entire 1D array so to use the pointer in 2D array we have to create a new pointer for every row.

Example
Let’s understand the relations between pointers and 2D array with the help of an example.

Code Implementation

  • C
#include <stdio.h>int main(void) { int arr[2][3] = {{31, 12, 11}, {81, 91, 110}}; int *p[2]; p[0] = arr[0]; p[1] = arr[1]; for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { printf("%d ", p[i][j]); } printf("\n"); } return 0;}

Output

31 12 11 81 91 110 

Explanation of the above example
Here we have used pointers to traverse in the row and for every new row we have created a new pointer.

Examples of Two Dimensional Array in C

In this section, we will discuss various examples of two dimensional arrays in c.

Example 1: Taking the Input from the user and printing it.
In the above example, we will see the implementation of the above-mentioned example.

  • C
#include <stdio.h>void main (){ int arr[3][3],i,j; for (i=0;i<3;i++) { for (j=0;j<3;j++) { printf("Enter a[%d][%d]: ",i,j); scanf("%d",&arr[i][j]); } } printf("\n printing the elements ....\n"); for(i=0;i<3;i++) { printf("\n"); for (j=0;j<3;j++) { printf("%d\t",arr[i][j]); } }}

Input

Enter a[0][0]: 8Enter a[0][1]: 7Enter a[0][2]: 6Enter a[1][0]: 5Enter a[1][1]: 4Enter a[1][2]: 3Enter a[2][0]: 2Enter a[2][1]: 1Enter a[2][2]: 0

Output

printing the elements ....8 7 65 4 32 1 0

Explanation of the above example
In the above example we have declared an array of size 3 x 3. Then we have asked the user to give the input after receiving each input we have shown the matrix in the output.

Example 2: Summation in Two Dimensional Array in C
Now we will discuss the implementation of the above-mentioned example.

  • C
#include <stdio.h>int main(){ float a[3][3], b[3][3], result[3][3]; printf("Enter elements of 1st matrix\n"); for (int i = 0; i < 3; ++i) for (int j = 0; j < 3; ++j) { printf("Enter a%d%d: ", i + 1, j + 1); scanf("%f", &a[i][j]); } printf("Enter elements of 2nd matrix\n"); for (int i = 0; i < 3; ++i) for (int j = 0; j < 3; ++j) { printf("Enter b%d%d: ", i + 1, j + 1); scanf("%f", &b[i][j]); } // adding corresponding elements of two arrays for (int i = 0; i < 3; ++i) for (int j = 0; j < 3; ++j) { result[i][j] = a[i][j] + b[i][j]; } // Displaying the sum printf("\nSum Of Matrix:\n"); for (int i = 0; i < 3; ++i) for (int j = 0; j < 3; ++j) { printf("%.1f\t", result[i][j]); if (j == 2) printf("\n"); } return 0;}

Input

Enter elements of 1st matrixEnter a11: 1Enter a12: 2Enter a13: 3Enter a21: 4Enter a22: 5Enter a23: 6Enter a31: 7Enter a32: 8Enter a33: 9Enter elements of 2nd matrixEnter b11: 9Enter b12: 8Enter b13: 7Enter b21: 6Enter b22: 5Enter b23: 4Enter b31: 3Enter b32: 2Enter b33: 1

Output

Sum Of Matrix:10.0 10.0 10.010.0 10.0 10.010.0 10.0 10.0

Explanation of the above example
In the above example we have taken two matrixes as input and then have added the two matrixes and printed the resulting matrix as output.

Common Applications of 2D Arrays

2D arrays in c have many applications that make them so useful. Some of the applications are explained below.

  • Storing and manipulating images or other graphical data.
  • Manipulating and storing matrices in linear algebra computations.
  • Implementing board games like checkers and chess.
  • Manipulating and representing data in spreadsheet programs.

Conclusion
Two-dimensional arrays in C serve as powerful tools for handling structured data efficiently. Understanding their syntax, declaration, initialization, and manipulation is crucial for developing applications that involve matrices, tables, and grid-based datasets. Mastery of these arrays empowers programmers to efficiently work with multidimensional data structures, facilitating various computational tasks and data processing operations in C programming.

FAQs (Frequently Asked Questions) about Two-Dimensional Arrays in C:

Below are some of the frequently asked questions on two dimensional arrays.

1. How do you declare a two-dimensional array in C?
To declare a two-dimensional array in C, you specify the data type of the elements and the size of rows and columns using the syntax: data_type array_name[rows][columns];.

2. How are elements accessed in a two-dimensional array?
Elements in a two-dimensional array are accessed using indices for rows and columns, like array_name[row_index][column_index];.

3. Can the size of rows and columns in a two-dimensional array be dynamic in C?
In C, the size of arrays is determined at compile time. However, you can simulate dynamic behavior using pointers and dynamic memory allocation.

4. How can you initialize a two-dimensional array in C?
You can initialize a two-dimensional array during declaration by providing the initial values in a nested brace-enclosed list for rows and columns, such as int array[2][3] = {{1, 2, 3}, {4, 5, 6}};.

5. What are the common operations performed on two-dimensional arrays in C?
Common operations include traversing the array elements, performing matrix operations (addition, multiplication, etc.), finding specific elements, and manipulating data within the array.

6. Can a two-dimensional array store elements of different data types in C?
No, in C, all elements within an array must be of the same data type.

7. How is memory allocated for a two-dimensional array in C?
Memory for a two-dimensional array in C is allocated in a contiguous block, calculated as the product of rows and columns multiplied by the size of the data type.

Two Dimensional Array in C - Syntax, Declaration & Examples (2024)
Top Articles
Latest Posts
Article information

Author: Corie Satterfield

Last Updated:

Views: 5876

Rating: 4.1 / 5 (62 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Corie Satterfield

Birthday: 1992-08-19

Address: 850 Benjamin Bridge, Dickinsonchester, CO 68572-0542

Phone: +26813599986666

Job: Sales Manager

Hobby: Table tennis, Soapmaking, Flower arranging, amateur radio, Rock climbing, scrapbook, Horseback riding

Introduction: My name is Corie Satterfield, I am a fancy, perfect, spotless, quaint, fantastic, funny, lucky person who loves writing and wants to share my knowledge and understanding with you.