Arrays in C

arrays in c

Introduction to array

An array is a collection of similar type of element storable together in a single contagious memory location. The important thing to remember is the all the data elements stored in an array should be of similar data type.

Array is a reference type which uses the Heap Area of memory to store the same type of data items and uses continues memory locations to store every data item of array.

Array

All data items in array are stored in a certain order in memory and for each element to identify uniquely, a Zero Based Index Number is specified with it. Also Array is a fixed Data Structure. So once the size of the Array is defined, then the size of array can not be modify.
  • Whatever the data type of the array can be.
  • The variables of array are stored on their near memory location.
  • The initialization of array starts with ‘0’.

Creating C array:-

Whenever you create an array, you define the name of the array and how many values it is going to store in it. As if you want to store 4 numbers, you can create an array for that. The general structure of creating arrays  in C is given below.
data_type array-name[size];
By size, you define how many values you want to store.
int sum[5]
In this you can store any 5 integer values in it.

Initializing C array

We can initialize each element of array by using index. Here’s the example:-
sum[0]=30;  // initialization of array
sum[1]=50;
sum[2]=24;
sum[3]=60;
sum[4]=78;
Initilization of array

C Array Example:-

#include
int main()
{
     int i=0;
     int sum[5];  // declaration of array
     sum[0]=30;  // initialization of array
     sum[1]=50;
     sum[2]=24;
     sum[3]=60;
     sum[4]=78;
// traversal of array
     for(i=0;i<5;i++)
     {
         printf("%d\n",sum[i]);
     }
//end of the loop
     return 0;
}

Output:

30
50
24
60
78

Types of arrays

types of arrya

Single Dimensional Array

In an array we don’t need to declare each and every element we only need to declare the array, the type of data in the array and the size of the array. Let’s see an example for declaring and saving element in the array and finally printing the elements saved.

#include<stdio.h>
int main()
{
   int arr[5];       //declaring the array
   int i,a;
   for(i=0;i<5;i++)    //loop to take input in the array from user
   {
       scanf("%d",&arr[i]);
   }
   for(i=0;i<5;i++)    // loop to print the elements of the array
   {
       printf("%d\n",arr[i]);
   }
   return 0;
}

Output :

1
2
3
4
5

Note : An array of size 5 can store 5 element but the 1st element is stored in the zeroth index value and then 1st, 2nd, 3rd, and last at 4th. And this goes for every array.

One dimensional array

Initializing one dimensional array:-

There are a bunch of way to store data in an array, like
int marks[5];
arr[0]=19;
arr[1]=11;
arr[2]=14;
arr[3]=8;
arr[4]=45;
OR
int marks[5]={19,11,14,8,45};
Both of these are the same array with the same elements.This type of liner array is called 1-D array 

Multi Dimensional Array

A multi-dimensional array or a 2-D array is a similar to 1-D array but the element stored in a 2-D array are stored in a matrix format rather than a linear manner. The elements are stored in rows and columns like a matrix.

The simplest form of multi-dimensional array is 2-D array.

 

Initializing of  2-D Array:-

Let’s see an example to understand.

int arr[3][4]={1,2,3,4,2,3,4,5,3,4,5,6}; or        //this array has 3 rows and 4 columns

int arr[3][4]={{1,2,3,4},{2,3,4,5},{3,4,5,6}};

Here’s the array will be like:-

1 2 3 4
2 3 4 5
3 4 5 6
multi dimensional array

Example of Multi-dimensional array:-

Code
#include
int main()
{
    int arr[3][4]={{1,2,3,4},{2,3,4,5},{3,4,5,6}};
    int i,j;
    for(i=0;i<3;i++)
    {
         for(j=0;j<4;j++)
         {
              printf("[%d][%d]=%d\n",i,j,arr[i][j]);
         }
    } 
    return 0;
}

output:-

[0][0]=1
[0][1]=2
[0][2]=3
[0][3]=4
[1][0]=2
[1][1]=3
[1][2]=4
[1][3]=5
[2][0]=3
[2][1]=4
[2][2]=5
[2][3]=6

Most of the array is created in 1D or 2D array.