C Programming: From Basic Syntax to Advanced Concepts
Implementing 2D Arrays in C
Introduction
Multidimensional arrays are supported by the C programming language. Here is a declaration for a multidimensional array in its generic form:
For instance, the declaration that follows produces a three-dimensional integer array.
2D Arrays Introduction
A two-dimensional array is a nested array of 1D arrays. You would write the following to define a two-dimensional integer array of size [x][y]:
Where type can be any legal data type recognised by C, and arrayName is a legal C identifier. A table with x number of rows and y number of columns can be compared to a two-dimensional array. Here is an example of a two-dimensional array with 3 rows and 4 columns:
Since "a" is the name of the array and "I" and "j" are the subscripts that specifically identify each element in "a," each element in the array is addressed with notationa[i][j].
Initializing Two-Dimensional Arrays
The initialization of multidimensional arrays is done by giving square brackets values for each row.
The targeted row may be identified without using the nested brackets. The next initialization is identical to the preceding one:
Accessing Two-Dimensional Array Elements
The row position and column index of a two-dimensional array are used to access individual elements inside the array. For instance,
The above line will choose the fourth member from the array's third row. It is seen in the figure up top. Check out the program below, where we utilized nested loops to manage a two-dimensional array.
Arrays can have any number of dimensions, as was previously stated, but it is probable that the majority of the arrays you construct will only have one or two dimensions.
Summary
- You learned about the array data structure in this article. It is a collection of discrete components of comparable kinds that may be stored in a single location.
- A one-dimensional array keeps track of a single list of different elements with related data types.
- An array of 2D arrays makes up a three-dimensional array.
- Each kind of array may be initialized at the time of declaration, and we can then access any of its entries.