What is Array?

  • A normal variable can hold a single value at a time. Say for example if we declare a variable of integer type, it can store any single integer value at a time.
  • It cannot be helpful if we want to store large amount of data.
  • C Programming language provides a great feature called “Array” that can be used when we need to deal with large amount of data.

An array is a collection of similar data items that share a common name.

  • We can think of an array variable as a container that can hold multiple values at a time.
  • In other words, an array is a group of related data items that are referred by common name.
  • Individual elements of array are known as Array elements.
  • Every array element is allocated a unique identifier number to refer it. It is referred as array index.
  • A specific element in an array is accessed by an index.
  • Array index always starts from 0 and ends to n-1 that means first element of an array will be stored on index 0, second element on index 1 and so on. The last element will have index n-1 where n is the size of the array.
  • In C, arrays elements are allocated contiguous memory blocks.
  • The lowest address corresponds to the first element and the highest address to the last element.
  • Arrays can be divided into the following types or dimensions.
  1. Single -Dimension Arrays (1D-Array)
  2. Two- Dimension Arrays (2D-Array)
  3. Multi Dimension Arrays

 

  1. Single -Dimension Arrays (1D-Array)

  • “An array with only one subscript is known as the one-dimensional array or single-dimension array”
Declaration of Single-Dimension Array:
  • We can declare single dimension array using the following syntax:
<data-type> arr-var-name [size]  ;
  • Like other variables, arrays must be explicitly declared so that the compiler can allocate space for them in memory.
  • Here, <data-type> specifies type of the array, which is the data type of each element in the array such as int or float
  • Array can store only similar data type elements that means if we declare an array of integer than all elements inside the array will have to be of type integer. If it is declared as float then all elements will be of float type and character if array is declared of character type. You can read about Array of character in our article “Introduction to String”.
  • arr-var-name specifies name of the array variable to refer it. It can be any valid and meaningful name and it should follow all the naming convention rules.
  • Size defines how many elements the array will hold that is number of elements to store.
  • Size is defined inside the ‘[]’ brackets.
  • In C89, the size of an array must be specified using a constant expression. Thus, in C89, the size of an array is fixed at compile time. (C99 allows arrays whose sizes are determined at run time).
  • For example, to declare an array called marks of type integer with size of 5, use this statement:
int marks[5];
  • It can store maximum 5 integer elements that means we can store 5 different integer values inside it.
  • The subscript (index) of array starts with 0 to 4 (size –1, i.e 5-1).
  • Computer assigns continuous memory location to it. In our case ‘marks’ array has assigned 5 continuous memory locations.

Note: In C program using Turbo C/C++ sometime it’s allowed to enter the array elements greater than the given size but it depends on your system. If there is space in memory then it doesn’t produce any garbage value otherwise it gives garbage value or may be your Turbo C/C++ editor closed.

Initialization of single Dimension Array:

  • Array can be initialized either statically or dynamically.
Static initialization:
  • We can initialize the above marks array variable either individually or in a group as follow:
marks [0] = 65 ; 
marks [1] = 78 ;
marks [2] = 85;
marks [3] = 55 ;
marks [4] = 72 ;
  • We can combine array declaration and initialization into a single statement as follow:
int marks [5] = {65, 78, 85, 55, 72 } ;
  • We can skip size if all elements are initialized at the time of declaration.
  • The above statement will declare array called marks of integer types with the size of 5.
  • Group initialization is performed using curly braces with each value separated by comma.
  • It will assign first value to index 0, second value to index 1 and so on.
  • Memory representation of array marks can be given as follow:

Array Representation

Note: If you initialize less no. of elements than the given size then other elements are by default initialized to zero. For example, below the array is of size 5 but we have initialized only 2 elements so only first two elements are initialized other will be initialized by zero.

int  x[5] = { 10, 50 } ;
Accessing single Dimension Array Elements:
  • We can access each array elements by using its index within square brackets along with array variable name.
  • For example, to access the first element of the array marks, you can use marks [0], for second element marks [1], and so on.
  • The following example performs total of array elements of marks by accessing them using their index.
total = marks [0] + marks [1]  + marks [2] + marks [3] + marks [4] ;

Note: If you provide the index out of the given size then it will not generate any error message but it will give the unexpected output during execution. For example if array is of size five and we try to access or assign with the index greater than the size it will generate unexpected results.

int  x[5] = { 10, 20, 30, 40, 50 } ;
printf (“\n %d “, x[7] ) ;
Dynamic initialization:
  • We can also initialize array at run time. It is referred to as dynamically initialization.
  • The following example uses for loop to initialize marks array from user at run time.
for ( i=0 ; i < 5 ;  i++ )
{
    printf(“\n Enter marks[%d]:”, i ) ;
    scanf( “ %d “ , &marks [i] ) ;
}
  • The above for loop is executed for five times which is the size of our array marks and reads five value from the user and each time counter variable gets incremented and based on its value it is assigned in array marks i.e. first value will be stored to marks [0] (as initially i is 0), second value will be stored to marks [1] (as i = 1) and so on.
  • The following program demonstrates reading marks of five subjects into array and displaying on the screen.
/* Program to read marks of five subjects into Array */

#include <stdio.h>
void main(void)
{
   int marks[5];    /* this declares integer array of 5 elements*/
   int i;
   
   clrscr();

   /* read marks from the keyboard  */

   for ( i=0 ; i < 5 ;  i++ )
   {
      printf(“\n Enter marks[%d]:”, i ) ;
      scanf( “ %d “ , &marks [i] ) ;
   }

   /* display contents of marks */

   for ( i=0 ; i < 5 ;  i++ )
   {
      printf( “\n %d  ", marks[ i ] ) ;
   }
}
  • Initialization of array has two drawbacks or limitation:
  1. There is no way to initialize selected array elements.
  2. There is no any shortcut method to initialize the large no. of array elements.
  • The amount of storage required to store an array is directly related to its type and size.
  • For a single dimension array, the total size in bytes is computed as follow:

total  bytes  =  sizeof (base  type)  ×   length  of  array

  • For example, the given integer array x with size of five requires total 10 bytes of memory space:
int x[5] ;

total bytes = sizeof (2 )  (since int requires 2 bytes)  x 5 (array size)

  • C has no bounds checking on arrays. You could overwrite either end of an array and write into some other variable’s data or even into the program’s code.
  • As the programmer, it is your job to provide bounds checking where needed.
  • For example, this code will compile without error, but it is incorrect because for loop will cause the array count to be overrun.
int count[10], i;

/* this causes count to be overrun */

for ( i=0 ; i < 100 ; i++)
     count[i]  =  i ;

Leave a Reply

Your email address will not be published. Required fields are marked *