Array with its type, explanation and example

An array is a collection of similar types of data (like int, float, or char), which is stored in contiguous memory locations.

We can define an array in C as

int arr[50];

It means an array arr that has the capacity to hold 50 integers. The benefits of an array are, now we don’t need to write int variable_name 50 times to store the 50 integer variable. All elements hold by the Array is accessed by the index number. Index of an array starts from 0 to Size_of_Array-1, and each index has some value.

In the above example in array arr[50], arr[0] is the first element of this Array, arr[1] is second and ………….., arr[50] is the last element.

The memory address of the first element of an array is called the beginning/opening address.


We can store elements in an array in a different way.

Example 1:

int arr[50];
arr[0] = 06;
arr[1] = 22;
arr[2] = 30;
arr[3] = 56;
.
.
.
.
arr[49] = 11;

Example 2:

int array[5] = {10,12,44,21,54};

Example 3:

for(int i = 0;i<50;i++){
  scanf("%d", &arr[i]);
}

This loop will run 50 times and will store data in an array. We can print the elements of the array directly by accessing the index number.

printf("%d",arr[index_number]);

or

for(int i = 0;i<100;i++){
  printf("%d", arr[i];
}

1-Dimensional or Single Dimensional Array

Array Declaration:

An array is declared with the bracket punctuators [ ], as shown in the following syntax:

Data_Type Array_Name[Number_Of_Elements] 

The below example shows a declaration of an array having the capacity to hold 50 elements of type integers, and the name of that Array is arr :

int arr[50];

Initializing the Array: 

We can initialize an array in C either one by one or using a single statement as follows −

One by one example:

int arr[50];
arr[0]=12;
arr[1]= 43;
arr[2] = 14;
.
.
.
.
.
arr[49] = 54;

Or using single statement:

int arr[5] = {10,32,11,45,38};

C Program to implement One dimensional Array

#include <stdio.h> 
int main () { 
   int n[50]; 
   int i,j;          
   for (i=0; i<50; i++) {
      n[i] = i + 50;
   }
   for (j=0; j < 5; j++ ) {
      printf("Element[%d] = %d\n", j, n[j] );
   }
   return 0;
}

Output

Element[0] = 50
Element[1] = 51
Element[2] = 52
Element[3] = 53
Element[4] = 54

Two Dimensional Array or Matrix

We can define a two-dimensional array as an array of arrays. In Two Dimensional Array, elements of the Array are arranged in the form of rows and columns. This is also used for representing the Matrix.

A Two Dimensional Array uses the two subscripts for declaring the elements of the Array.

For example :

int arr[5][5]

Above is the Example of the Two Dimensional Array where the first 5 represents the total number of Rows and the Second 5 represents the total number of Columns. And by multiplying this number of rows and columns, we can find the total number of elements that are stored by two-dimensional arrays.

Declaration of two dimensional Array in C

Data_Type Array_Name[rows][columns];

data_type: It defines the type of data that can be held by an array. Here data_type can be int, char, float. etc. 

array_name: Name of the array 

rows: number of the rows 

columns: number of the columns

Consider the following example

int arr[5][4];

Here, 5 is the number of rows, and 4 is the number of columns.

Initialization of 2D Array in C

We can initialize a two-dimensional array in the following way.

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

Declaration and Initialization

int a[3][4] = {  
   {0, 1, 2, 3} ,   /*  initializers for row indexed by 0 */
   {4, 5, 6, 7} ,   /*  initializers for row indexed by 1 */
   {8, 9, 10, 11}   /*  initializers for row indexed by 2 */
};

Two-dimensional Array Implementation in C

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

Output

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

Multi-dimensional Arrays

We can define a Multi-dimensional array as an array of arrays. Two Dimensional arrays is also a multi-dimensional array. In a Multi-Dimensional Array, elements of an array are arranged in the form of rows and columns.
In multi-dimensional arrays, data are stored in tabular form, also known as in row-major order.

Declaring Multi-dimensional arrays

data_type array_name[size1][size2]….[sizeN];

data_type: It defines the type of data that can be held by an array. Here data_type can be int, char, float, etc.

array_name: Name of the Array

size1, size2,… ,sizeN: Sizes of the dimensions

Initialization of the multidimensional array

int test[2][3][4] = { 
                     { {5, 6, 1, 9}, {10, -3, 91, 61}, {73, 32, 83, 22} },
                     { {13, 43, 56, 36}, {25, 9, 93, 55}, {39, 21, 74, 6} }
                 };

Multidimensional Dimensional array in C

#include<stdio.h> 
int main()
{
    int i, j, k, arr[2][3][4];
    printf("Enter 24 values for 3-D array: \n");
    for(i = 0; i < 2; ++i) {
        for (j = 0; j < 3; ++j) {
            for(k = 0; k < 4; ++k ) {
                scanf("%d", &arr[i][j][k]);
            }
        }
    } 
    printf("\nDisplaying the values of 3-D array:\n");
    for(i = 0; i < 2; ++i) {
        for (j = 0; j < 3; ++j) {
            for(k = 0; k < 4; ++k ) {
                printf("arr[%d][%d][%d] = %d\n", i, j, k, arr[i][j][k]);
            }
        }
    }
    return 0;
}

Output

Enter 24 values for 3-D array: 
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
1
2
3
44
5
6

Displaying the values of 3-D array:
arr[0][0][0] = 1
arr[0][0][1] = 2
arr[0][0][2] = 3
arr[0][0][3] = 4
arr[0][1][0] = 5
arr[0][1][1] = 6
arr[0][1][2] = 7
arr[0][1][3] = 8
arr[0][2][0] = 9
arr[0][2][1] = 1
arr[0][2][2] = 2
arr[0][2][3] = 3
arr[1][0][0] = 4
arr[1][0][1] = 5
arr[1][0][2] = 6
arr[1][0][3] = 7
arr[1][1][0] = 8
arr[1][1][1] = 9
arr[1][1][2] = 1
arr[1][1][3] = 2
arr[1][2][0] = 3
arr[1][2][1] = 44
arr[1][2][2] = 5
arr[1][2][3] = 6