Tutorial Playlist
An array’s dimension represents a specific direction in which the array elements can be manipulated. In C, an array with two dimensions is called a two dimensional array.
Let’s dive in to explore more about two dimensional arrays and their accurate implementation through various examples.
A two dimensional array in C is an array of multiple one-dimensional arrays. It holds an array of different arrays, an array of different one-dimensional arrays, and a list of different lists. It is represented as a matrix comprising rows and columns or an array of arrays.
-Two dimensional array in C help you to store and work on data in a tabular format.
-They are useful for representing matrices, tables, grids, or other tabular data structures.
-They are stored in contiguous memory locations. So, they facilitate efficient memory utilisation.
Syntax for declaring Two Dimensional Array
Here’s the syntax for declaring a two dimensional array in C:
data_type array_name[i][j] |
Here, i and j represent the size of the two dimensions; i shows the number of rows and j shows the number of columns.
Here’s an example of a two dimensional array:
int X[5][7]; |
The above command declares a two-dimensional array named X with 5 rows and 7 columns.
You can initialize a two dimensional array in C in one of the below two ways:
Method-1: Initializing without nested braces:
The syntax for this method is:
int Ar[x][y] = {element 1, element 2, ... element xy} |
Based on the above syntax, here’s the two-dimensional array example:
int Ar[2][3] = {2, 3, 5, 7, 8, 9} |
Method-2: Initializing with nested braces:
The syntax for this method is:
int Ar[x][y] = {{ element 1, element 2, .. element y} , {......} , {..., element xy-1, element xy}}; |
Example:
int Ar[2][3] = {{5, 2, 3}, {7, 9, 8}}; |
The memory for a two-dimensional array in C is assigned in an adjacent block. The concept it uses for the same is “row-major order”.
The following section shows how the memory is allocated for an int array[3][4].
arr[0][0] arr[0][1] arr[0][2] arr[0][3] |
Here’s the two dimensional array syntax for accessing elements.
array_name[i][j] |
In the given example, if you have an array X[10][20], and you want to access the element in the 5th column and 4th row, the correct syntax would be X[3][4].
Since C uses zero-based indexing, the 4th row corresponds to index 3, and the 5th column corresponds to index 4.
Therefore, the correct syntax for accessing the element at the 4th row and 5th column in a two-dimensional array named X would be X[3][4].
Addition and Subtraction of Two Dimensional Arrays
You must iterate on the elements of an array and implement the addition and subtraction operations to perform the addition and subtraction of a two dimensional array in C. The following example shows addition and subtraction of two dimensional arrays:
#include <stdio.h> |
Output:
Array-1: |
If you want to multiply a two dimensional array in C, you must iterate over the arrays’ elements and implement the necessary arithmetic operations. Let’s look at the following example.
#include <stdio.h> |
Output:
Array-1: |
You must interchange the columns and rows of the array if you want to determine the transpose of a two dimensional array in C.
void functionName(dataType arrayName[][numCols], int numRows) { |
Here, dataType represents the data type of the elements defined in the array, ArrayName represents the array’s name, and NumCols represents the number of columns in a particular array.
#include <stdio.h> |
Output:
1 5 9 |
In the above example, we define a function printArray that accepts a two-dimensional array. It accepts the number of rows and columns as parameters. We use nested loops in the function to traverse the array and display its elements.
You can pass a two dimensional array to a function by mentioning the array dimensions within the function parameter declaration.
In C, you can use the malloc function and pointers to dynamically allocate memory for a two dimensional array.
datatype **arrayname; |
#include <stdio.h> |
Output:
123 |
If you want to free the memory allocated for any two dimensional array in C, you must separately deallocate memory for each row and then free the memory for the array.
A multidimensional array in C is an array comprising multiple dimensions. It is essentially an array of arrays.
dataType arrayname[size1][size2]...[sizeN]; |
#include <stdio.h> |
Output:
1234 |
The above example defines a three-dimensional array arr with dimensions S1, S2, and S3. These dimensions are 2, 3, and 4, respectively. We use nested loops to assign values to the array’s elements. The output displays the array after iterating on each dimension through nested loops.
To access a multidimensional array’s elements in C, you need to use multiple sets of square brackets to declare each dimension’s indices.
-Thoroughly determine the number of columns and rows in an array. It helps you to correctly allocate memory, iterate over the array, and access elements.
-Before using an array, always initialize it with proper values.
-Make sure the indices you use fall in the valid range of the array.
-Don’t access the elements outside the acceptable range of the array.
-If you are dynamically allocating memory for your two dimensional array using malloc, make sure to correctly allocate memory for both the columns and rows.
-Use the proper data type for your array’s elements depending on the data to be sorted.
This tutorial familiarises you with definition, declaration, operations, and many other aspects of two dimensional array in C. For several reasons, It is essential to understand two dimensional arrays in C programming. These include the ease of accessing and processing data, effectively handling multidimensional data structures, representing and manipulating grid-like data structures, etc.
Learning through tutorials is one of the most effective ways to strengthen your C fundamentals. You can pursue upGrad’s Full Stack Software Development Bootcamp, which imparts the necessary skills to stay competitive in the tech industry.
Enroll now to commence your journey!
1. Can a two dimensional array have different row sizes in C?
No, a two dimensional array in C should have a fixed number of rows and columns. Each row should have the same number of columns. You can’t declare a two dimensional array with different row sizes.
2. Can you loop through a two dimensional array in C?
Yes, you can loop through a two-dimensional array with the help of nested loops. The external loop iterates over the rows whereas the inner loop iterates over the columns.
3. Are two dimensional arrays stored in column-major or row-major order in memory?
The two dimensional arrays are stored in row-major order in memory in C. It implies that a row’s elements are consecutively stored in memory, followed by the next row’s elements, and so on. This sequence enhances cache utilisation and makes memory access more efficient.
PAVAN VADAPALLI
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...