-->

Types of array in C

3 minute read

 Types of array in C

What is array?
An array is a collection of data of the same data type. It is a one type of data structure that can store a fixed-length sequential collection of elements of the same data type. 
Instead of declaring individual variables, such as number0, number1, ..., and number50, you declare one array variable such as numbers[50] to represent individual variables. A specific element cab be accessed by an index.
All arrays consist of contiguous memory locations. The first element of array is array[0] and the last element of array is array[n-1].
There are three types of array
 1)   single dimensional array
 2)   two dimensional array
 3)   multi dimensional array

   1)  single dimensional array

Declaring Arrays

Syntax 
type arrayName [ arraySize ];
This is called a single-dimensional array. The arraySize must be an integer constant and type can be any valid C data type. For example, to declare a 20-element array 
int balance[20];
Here size of an array is 20. which is sufficient to hold up to 20 integer numbers.

Initializing Arrays

We can initialize an array in C using a single statement as follows 
float balance[5] = {1000.0, 2.0, 3.4, 7.0, 40.0};
The number of values between braces { } cannot be more than the number of elements that we declare for the array size between square brackets [ ].
If we remove the size of the array, an array just big enough to hold the initialization is created. Therefore, we write
float balance[] = {1000.0, 2.0, 3.4, 7.0, 40.0};
Following is an example to change a single element of the array. 
balance[4] = 50.0;
The above statement change the 5th element in the array with a value of 50.0.  

Accessing Array Elements

An element is accessed by the indexing with the array name. 
For example 
float salary = balance[4];
The above statement will take the 5th element from the array and assign the value to salary variable.
Example
#include<stdio.h>
void main()
{
    int no;
    int arr[] = {1, 2, 3};      // Compile time array initialization
    for(no = 0 ; no < =2 ; no++) 
    {
        printf("%d\t",arr[no]);
    }
}

Output
1 2 3

Advantages of an Array in C:
1.   Random access of elements using array index.
2.   Use of less line of code as it creates a single array of multiple elements.
3.   Easy access to all the elements.
4.   Traversal through the array becomes easy using a single loop.
5.   Sorting becomes easy as it can be accomplished by writing less line of code.

Disadvantages of an Array in C:
1.   Allows a fixed number of elements to be entered which is decided at the time of declaration. Unlike a linked list, an array in C is not dynamic.
2.   Insertion and deletion of elements can be costly since the elements are needed to be managed in accordance with the new memory allocation.

   2) Two Dimensional Array in C
It can be defined as an array of arrays. The 2D array is organized as matrices which can be represented as the collection of rows and columns. However, 2D arrays are created to implement a relational database lookalike data structure.

Declaration of two dimensional Array in C

Syntax
data_type array_name[rows][columns];  

 Example.

int twodimen[4][3];  
Here, 4 is the number of rows, and 3 is the number of columns.

Initialization of 2D Array in C

In the 1D array, There is no need to specify the size of the array if the declaration and initialization are being done simultaneously. However, this will not work with 2D arrays. We have to define at least the second dimension of the array. The two-dimensional array can be declared and defined as follows.
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};

Program

Storing elements in a matrix and printing it.

#include <stdio.h>    
    void main ()    
   {    
    int arr[3][3],i,j;     
    for (i=0;i<3;i++)    
    {    
        for (j=0;j<3;j++)    
        {    
            printf("Enter a[%d][%d]: ",i,j);                
            scanf("%d",&arr[i][j]);    
        }    
    }    
    printf("\n printing the elements ....\n");     
    for(i=0;i<3;i++)    
    {    
        printf("\n");    
        for (j=0;j<3;j++)    
        {    
            printf("%d\t",arr[i][j]);    
        }    
    }    
}    
Output
Enter a[0][0]: 56   
Enter a[0][1]: 10   
Enter a[0][2]: 30  
Enter a[1][0]: 34  
Enter a[1][1]: 21 
Enter a[1][2]: 34    
Enter a[2][0]: 45
Enter a[2][1]: 56
Enter a[2][2]: 78   
 
 printing the elements .... 
 
56      10      30  
34      21      34  
45      56      78