C Programming

Array in C

Array is one of the most important topics in the C language. We can store the same type of data elements in a contagious memory allocation through an array. The special feature of an array is we can store a group of variables in the same name but in different index no. Index means the address of each memory block. So it has three main features. They are:

  1. Array is a linear collection of similar elements.
  2. Array is also known as a subscript variable.
  3. Array is a group of variables.

Where can we use the Array?

Here we have to evaluate the average of 1000 values. So, we have to declare 1000 variables minimum to complete this program. Instead of declaring 1000 separate variables, we use an array.

EX: a [1000] where a [] an array and 1000 is the no of variables.

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

It is a group, but it has no individual name, but it has index numbers just like 0, 1, and 2, and it takes contiguous memory in RAM. Index no. is also called Position no. It has the same type of data element, just like either int, or char, or float.

Array actually represents the index of the first variable in an Array.

If we want to access each variable, we write

a [0] [At first the name of the array, then square bracket and index no.]

But at first, when we declare an array, that means total no. of variables in an array,

int a [10] [It means a is an array, where 10 variables exist]

But after that, when we write just like,

a[0], a[1]…. it means index no. of the array

Let a [0] = 12, a [1] =8, a [2] =10, a [3] =15

Example-1

#include<stdio.h>

Here we show how an array is declared. With the help of an array, we calculate the average of some values.

int main()
{
    int a [ 10 ] , sum = 0 , i ;    // declaring an array.
    float avg;
    printf ( " Enter 10 numbers " ) ;
    for ( i = 0 ; i <= 9 ; i++ )
    {
        scanf("%d",&a[i]);  // taking some from the user in the array.
    }
    for ( i = 0 ; i <= 9 ; i++ )
    {
        sum = sum + a [ i ] ;
    }
    avg = sum / 10.0 ;
    printf ( " average is %f \n ",avg ) ;
    return 0 ;
}

Output

Explanation

Here we declare an array named a[], which holds 10 values. With the help of for loop, we add some values which will be taken from the user using the scanf () function. Then we calculate the average of this sum of ten values and store it in the avg variable.

Declaration of Array

Int a []; error [Whenever we declare an array without initialisation and do not put any number between the bracket, it means error occurred here]

Int a [5];

a [0] =9;

int a [5] = {9,6,8,0,2}

int a [] = {9,6,8,0,2}

int a [5] = {9,6,8,0,2,4,7} It is an error, as the array is out of bound

A [5] = {9,6}

Whenever we declare an array without initialization, it means each index has, by default garbage value existing there.

When we declare an array with initialization, then we are allowed to empty the square bracket.

If we initialize an array with less number, the rest of the values in the index of the array are, by default, zero.

Example-2

Here we show how a dynamic array works on the data, and with the help of the array, we evaluate the minimum and maximum value from a group of numbers.

#include<stdio.h>

#include<stdlib.h>

int main()
{
    int *a , i , n , min , max ;

    printf ( " Enter size of the array : " ) ;
    scanf("%d",&n);
    a = ( int*) malloc ( n * sizeof ( int ) ) ; // malloc () is called to create an array.

    for ( int i = 0 ; i <= n - 1 ; i++ )
    {
        printf ( " arr[%d]: " , i ) ;
        scanf("%d",&a[i]);
    }
    min = max = a[0] ;
    for ( i = 1 ; i  a[i] )
          min = a[i] ;
        if( max< a[i] )
            max = a[i] ;        // find out the maximum value.
    }
    printf ( " minimum of array is : %d " , min ) ;
    printf ( " \n maximum of array is : %d \n " , max ) ;

    free(a) ;
    return 0 ;
}

Output

Explanation

Here with the help of the malloc () function, we declare an array and get some values on runtime time to calculate minimum and maximum values from those values. We declare two variables named min and max and print those variables.

Example-3

With the help of the array index, we reverse the values of the array here.

#include <stdio.h>

#include <stdlib.h>

int main()
{
    int *a , n ,temp , i ;

   printf ( " Enter size of the array : " ) ;
    scanf("%d",&n);
    a = ( int*) malloc ( n * sizeof ( int ) ) ;     // creating an array.

    for ( int i = 0 ; i <= n - 1 ; i++ )
    {
        printf ( " arr[%d]: " , i ) ;
        scanf("%d",&a[i]);
    }
    for ( int i = 0 ; i < n / 2 ; i++ )
    {
        temp = a[i] ;
        a[i] = a[ n - i - 1 ] ;
a[ n - i - 1 ] = temp ;
    }
printf ( " reverse of the array \n " ) ;        // reversing the array.
    for ( int i = 0 ; i < n ; i++ )
    {
        printf ( " %d, ", a[ i ] ) ;
    }
printf ( " \n " ) ;
`free(a) ;
    return 0 ;
}

Output

Explanation

Here we create an array with the help of dynamic memory allocation. Malloc () function creates the block of the array and dynamically gets some values. Then we reverse these values of the arrays and print them.

Example-4

Here we show how the elements of an array are copied to another array.

#include <stdio.h>

int main()
{
    int a[100] , b[100] ;       // declaring arrays.
    int i , n ;


       printf ( " \n\n Copy the elements one array into another array :\n " ) ;
printf(" ---------------------------------------------------- \n " ) ;

       printf ( " Input the number of elements to be stored in the array : " ) ;
       scanf("%d",&n);

       printf ( " Input %d elements in the array : \n " , n ) ;
       for ( i = 0 ; i < n ; i++ )
        {
          printf ( " element - %d : " , i ) ;
          scanf("%d",&a[i]);
        }
    /* Copy elements of first array into second array.*/
    for ( i = 0 ; i < n ; i++ )
    {
        b [ i ] = a [ i ] ;     // copying elements.
    }

    /* Prints the elements of first array   */
    printf ( " \n The elements stored in the first array are : \n " ) ;
    for ( i = 0 ; i < n ; i++ )
    {
        printf ( " %d ", a[i] ) ;
    }

    /* Prints the elements copied into the second array. */
    printf ( " \n\n The elements copied into the second array are : \n " ) ;
    for ( i = 0 ; i < n ; i++ )
    {
        printf ( " %d " , b [ i ] ) ;
    }
           printf ( " \n \n " ) ;
           return 0 ;
}

Output

Explanation

Here we declared an array named a []. In this array, we take some values from the user. Now we declared another array named b []. The elements of array b [] are copied from the elements of the array a [] and print those values both array a[] and b[]. We can see the same output is generated as they are copied versions.

Conclusion

Here we discuss in detail the basic concept of an array. An array is a procedure to store values in the memory. Through array, we can access any element quickly. But array has some limitations regarding its usefulness but is very useful in the programming perspective.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com