In this tutorial section we will learn how to Declare Two dimensional 2D array in C Language.
Array declaration specify the name of array its types and size of elements that is got stored in contigueous memory location.
2 D array declaration is similar to the declaration of one dimensional array , except that 2D array has two subscripts instead of one.
first subscript is for row and second is used for column..
Following is syntax for declaring two dimensional array in C Language.
typeName variableName[row][column];
This declares the 2D array with the specified size (row and column size).
variableName is the name of 2D array.
typeName is the data type used to declare 2d array.
row and column specify the size of 2d array. i.e total number of values in the array.
In the C language The 2D array is the array that can be organize in the column and row. Elements or values in two dimensional array is read by using indices.Indices is the position of rows and columns.
For example. In the 4x4 2D Array the element in the third row and fourth column is accessed using indices(2,3) or row 2, column 3.
Refer the following figure for more detail.
Two Two Dimensional 2D Array Declaration in C Language
2D Two Dimensional Array Declaration and Representation in memory.
Let us study the declaration of two dimensional array from the given figure.
The image above depicts the declaration of 2d array.
The Two Dimensional array is also called a matrix. the data type used can be of any type like int,float, char,double etc.
2D array in C programming can be defined as an array of array.
we have declared the 2D array int matrix[3][3] .
int is the data type of 2D array matrix . matrix is the name of the 2D array variable and [3][3] is the size of rows and columns.
The matrix given in the figure is represented as 3 rows and 3 columns.
The values in the two dimensional array are generally represented with the help of format arrName[i][j] where 'i' is the number of rows and 'j' is number of columns of 2D array.
The size of the 2D array is equal to the multiplication of number of rows and columns present in the array.
The matrix[3][3] two dimensional array is given in the figure. How ever in order to store the 2D array into the Memory the array need to be mapped to 1D array.
i. Conceptual Memory Representation:
Conceptual Memory Representation is the high level description that helps the design of memory in the programming.
The conceptual memory allocated is for the elements that we want to store in matrix.
in the given figure ,The allocated conceptual memory is for the elements those we want to store in matrix.
The first element in the given 3x3 matrix will be at the place matrix[0][0].
The fourth element in the given 3x3 matrix will be at the place matrix[1][0], i.e The element at 2nd row and first column.
The last element in the given 3x3 matrix will be at the place matrix[2][2], i.e The element at 3rd row and third column.
ii. Actual Memory Representation:
from the given figure named "Actual Memory representation", we can observe that the memory allocated for the two or multidimensional array is of contiguous type or linear type.
From the figure the Array matrix[3][3] is of integer type so each element would occupy 4 bytes of memory space that's the reason there is the difference of 4 in address.
The memory address are generally represented in hexa decimal number.but in the diagram it shown in the int format for our understading purrpose only so that we can understand that the address difference between each element is equal to 4( int size 4).
In given figure ,The allocated memory(Actual Memory Representation) is for the elements those we want to store in matrix.
The first element in the given 3x3 matrix will be placed at the position matrix[0][0].
The fourth element in the given 3x3 matrix will be stored at the place matrix[1][0].
The data values in the Two dimensional or multidimensional array are stored in linear storage. Linear storage type are to be categorised in two types.
a.Row major order: : In row-major order, we start storing the array elements in the first row followed by second row and so on.
Hence we can say that the
elements of the first row are stored linearly in the memory followed by the second row and so on. there is not any separation between the rows inside memory.
b.Column major order: In this method the elements are get stored in first coloumn followed by second column and so on. we can say Column major order is different or opposite to row-major for storing the data elements.So in this method or case, elements of the first column are stored linearly in the memory followed by the second column, and so on.
Previous Topic:-->> Two 2D array in C || Next topic:-->>Read and Display 2D array.
Other Topics:
Variables and Identifiers Relational Operators if-else statements Switch case While Loop Infinite while Loops C FOR Loop Infinite for Loops Continue in Loops One Dimensional Array Two Dimensional Arrays Read and Display 2D Arrays Types of functions Passing Array To Functions Nesting of Function Array vs Structure Array of Structure Structures and Functions Structures Within Structures Use Of Pointers In C File Handling In C Loops FAQ Arrays FAQ count vowels in a file Function FAQ Conditional Statements Assignments For Loops Assignments Arrays Assignments Function Assignments Structure Assignments Pointers Assignments Files Assignments Storage classes Assignments Binary Files count words,lines in a file Copy files Update File Continue in Loops break in Loops Difference Between While and Do while difference while do..while & for malloc calloc Storage Classes Operators MCQ Conditional Statements MCQ Loops MCQ Arrays MCQ Function MCQ Structure MCQ Pointers MCQ Files MCQ Storage classes MCQ