C Arrays

An array is a group of like-typed variables that are referred to by a common name. Array of any type can be created and may have one or more dimensions. Arrays offer a convenient means of grouping related information.

An elements of an array occupy a continuous memory location. Arrays in C language are static type and hence the size of array cannot be altered at runtime.

A specific element in an array is accessed by its index.  In C, array elements are indexed starting with 0, so that an array of size N is indexed from 0 to N − 1.

One-Dimensional Arrays

A one-dimensional array is, essentially, a list of like-typed variables.

Syntax to declare one-dimensional array is:

type var-name[SIZE];

e.g.

int month_days[12] ; 
int a[10];

The second declaration will defines an array a of size 10 i.e a block of 10 consecutive objects named a[0], a[1], a[2], … , a[9]. This can be presented in pictorial form as:

arrayFigure: Pictorial representation of an array

The element in array is accessed via index. The notation a[i] refers to (i +1) th element in array. For example, a[1] refers to second element.

Note: An array must contain at least one element, and it is an error to define an empty array.

double month_days[0]; /* Invalid. Won’t compile. */

Array Initialization

After declaring an array, it can be initialized element wise. For example:

int a [10];   //declaration
a[0] = 2 ;
a[1] = 4 ;
a[2] = 1 ;
...​
...​
...
a[8] = 42 ;
a[9] = 17 ;

The pictorial representation after initialization would be like:

array-initFigure: Array initialization

 An array can be explicitly initialized when it is defined by using an initialiser list. Initalizer list is a list of values of the appropriate type enclosed in braces and separated by commas.

For example,

int month_days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

If the size of an array with an initialiser list is not specified, the array will automatically be allocated memory to match the number of elements in the list. For example, in above initalization, the size of array month_days will be twelve. 

The size of an array can also be computed via the sizeof operator,

int size = sizeof(month_days); /* size equals 12 * sizeof(int) */

 

The following program explains more about an array.

//program to illustrate array and operation on elements
#include<stdio.h>
int main()
{
    double nums[] = {10.1, 11.2, 12.3, 13.4, 14.5};
    double result = 0;
    int i;
    for(i=0; i<5; i++)
    {
        result = result + nums[i];
        printf("%.2f \n",nums[i]);
    }
    printf("Average is %.2f",result / 5);
}

The output of the program is:

10.10 
11.20 
12.30 
13.40 
14.50 
Average is 12.30 

Here, array nums with 5 elements is defined and initialized with elements. Each of these element can be accessed via their index starting from 0 to 4 (N – 1 = 5 -1 = 4).

 

Two-Dimensional Arrays

A two-dimensional array is an array of an array. In similar ways, multi-dimensional array is an array of arrays.

A 2-D array is declared in the same way as 1-D array. The following declaration defines an array with size of 4 X 5.

int matrix [ 4 ] [ 5 ];

The above declaration will defines an array a of size 20 ( 4 X 5)  i.e a block of 20 consecutive objects named a[0][0], a[0][1], a[0][2], … , a[3][3], a[3][4]. This can be presented in pictorial form as:

array-2DFigure: Conceptual representation of 4 X 5 array

A multi-dimensional array is defined using multiple adjacent square brackets, and the elements of the array may be initialised with values enclosed in curly braces.

Here is example that defines 2-D array.

int matrix[3][4] = {
   { 2, 7, 9, 3 },
   { 6, 4, 1, 9 },
   { 2, 6, 4, 3 }
};

This statement defines an array of size 12 ( 3 X 4 ) and initializes the with values.

Here is a program to make the idea more clear about 2D array.

//program to illustrate 2D array
#include<stdio.h>
int main()
{
    int twoD[4][5];        //declares a 2-D array of size 4X5
    int i, j, k = 0;
    for(i=0; i<4; i++)     //outer loop is for rows
    {
        for(j=0; j<5; j++) //inner loop is for column
        {
            twoD[i][j] = k;
            k++;
        }
    }
    for(i=0; i<4; i++)
    {
        for(j=0; j<5; j++)
            printf("%d ",twoD[i][j]);
        printf("\n");
    }
} 

The output of the program is:

0  1  2  3  4 
5  6  7  8  9 
10 11 12 13 14 
15 16 17 18 19

Here, array twoD with 20 ( 4X5) elements is defined and initialized with elements. Each of these element can be accessed via their  index as presented in conceptual form. 

References : C programming language

C Recursion
C Pointers