Types of Arrays in C Programming

Types of Arrays in C Programming

Arrays are used to store related data items. So arrays are the very important concept to understand.

There are two types of arrays
1. One-dimensional Arrays
2. Multidimensional Arrays


there are two types of arrays one is linear and second is multidimensional array

One-Dimensional Arrays:

Linear arrays or simple arrays are known as one-dimensional arrays because in this type of arrays data store in one dimension. For example, if we declare an integer array of size 5 such as
int myFirstArray[] = {56,6,10,25,1};
the data will store in one dimension you can see in figure
one dimensional array memory representation
To read the value from the one-dimensional array we use one subscript. So also known as one subscripted array.

If we want to print 25 from our array we can use statement just like
printf("Value at index 3 : %d", myFirstArray[3]);
This will print the value of index 3 just like
Value at index 3 : 25

If I want to change the value of index 4, I will write
myFirstArray[4] = 78;
This will change the value at index 4 just as
after changing the value in one dimensional array
Read everything about One-dimensional Arrays.

Multidimensional Arrays:

In multidimensional arrays, we will discuss only two-dimensional arrays.
In two-dimensional arrays, data stores in two dimensions and we have two use two subscripts to access the particular block of the array to read the value or to store the value in arrays.

We can declare and initialize a two-dimensional array just like this
int arr[2][2] = {{1,2},{3,4}};
this array will have two rows and two columns.

The first subscript in the initialization of two-dimensional array shows the total number of rows and second shows the total number of columns.

The upper array will store the values in memory such as shown in figure
memory representation of two dimensional array
Two subscripts are used to access any block in two-dimensional arrays one is for row and second is for columns, So also known as the double subscripted arrays.

If I want to print the value 2 from our two-dimensional array we will use the statement like this
printf("Value at row 0 and column 1 : %d", arr[0][1]);
This will print the value of row 0 and column 1 just like
Value at row 0 and column 1 : 2
If I want to change the value with row index 0 and column index 1, then I will write
arr[0][1] = 10;
This will change the value at row index 0 and column index 1 just as
after changing the value in two dimensional array
Read more about Two-dimensional Arrays.

Comments

Popular posts from this blog

Change your Application background in Scene Builder || JavaFX FXML Tutorial

5 Programming Languages To Learn in 2019

c/c++ Programming questions || Practice Problems for Beginners || C Programs List