What is an Array and its types in C?

In this article, we will discuss what is an array and its types? If we want to store n values, then we need n variables. In general, we declare a variable to store a value. So, we need n variables to store n values. If we want to keep track of the salary of 50 employees we must declare 50 variables, which is not handy for our programming. Our program becomes more complex, and it reduces efficiency and readability. ARRAY can address this problem.

Table of Content
  1. Definition of Array
  2. Advantages of Array
  3. Drawback of Array
  4. Types of Array
    1. One Dimensional Array
    2. Two Dimensional Array
    3. Multi Dimensional Array

What is an Array

  • An array is a variable that can store multiple values of the same type.
  • There is an array of the same element (homogenous).
  • Array is a group of values.
  • Arrays are a collection of elements with the same data type and size.
  • You can use an array to store a list of numbers or a list of names.
  • A set of variables grouped to form an array.
  • It is also necessary to declare a subscript when declaring an array.

Advantages of Array

  • Less code to the access the data.
  • Multiple data items of the same type using only one name.
  • You can use it to implement other data structures like linked lists, stacks, queues, trees, graphs, etc.
  • It is easy to retrieve the elements of an array using the loop.
  • Using the array, we can access any element at random.

Drawback of an Array

  • We cannot exceed the size limit we set when we create the array. So, unlike Linked Lists, it doesn’t grow during use.
  • C does not check the bounds of array indexes.
  • Since we cannot change the size of an array, developers generally declare large arrays in case their data needs to grow in the future.

Types of Array

Following are the types of Array:

One Dimensional(1-D) Array

  • A single subscript represents each element of a 1D array.
  • Memory locations for the elements are consecutive.

Declaration of 1D Array

Syntax
data_type array_name[array_size];
Example
int marks[5];
Here, int is the data_type, marks is the array_name and 5 is the array_size

Initialization of 1D Array

  • An array will allocate the following types of memory blocks.
int a[5];
/* 5 is a subscript *
/
  • But at that point, the array contains meaningless (garbage) values.
  • Initialization refers to assigning values to an array.
  • The initialization uses curly brackets.
int a[5]={10,20,30,40,50}; //Array of Integers - Integer Array
char a[3]={'R','A','M'); //Array of Characters - Character Array
  • Characters are represented in a single quote otherwise they treat as a variable.
  • When the array’s size is less than the value assigned to it, zero 0 is assigned to the remaining values.
int a[5]={10,20,30};
10203000
  • You can omit the subscript or size of an array (not defined).
  • The compiler in this case provides enough space for all initialized elements.
int a[]={10,20,30};
10203000
char name[] = {'r','a','m','\0'); /* \0 is NULL value */
char name[]="ram";
  • The term “string” refers to an array of characters.

How to Access the Elements of a 1D Array?

  • As we know that an array can store more than one element.
  • It is then difficult to access an element since all elements have the same name.
  • As a result, each element in an array is assigned a unique position number that is called an “index”.
  • This index always and always starts from 0 to n-1 where n is the subscript of an array.
  • Consequently, this index number identifies the element.
int a[5]={10,20,30,40,50);
1020304050
a
Index
0 1 2 3 4 N = subscript of an array. Here n = 5 [So index range is 0 to 4.] 10 can be accessed ->a[0] 20 can be accessed ->a[1] 30 can be accessed ->a[2] 40 can be accessed ->a[3] 50 can be accessed ->a[4]

Scan or Enter a Value in a 1D Array

  • During runtime, we use scanf( ) to enter values into variables.
Locationscanf( ) Syntax
a[0]scanf(“%d”,&a[0]);
a[1]scanf(“%d”,&a[1]);
a[2]scanf(“%d”,&a[2]);
a[3]scanf(“%d”,&a[3]);
a[4]scanf(“%d”,&a[4]);
  • Here the user will input values one by one, and then store them in array locations.
  • It is not feasible to write scanf( ) for each element of an array when it has a lot of elements or large size.
  • Loops are also acceptable here as scanf( ) only changes the index. Since a loop is easier to use here, it can simplify the process.
for(i=0;i<5;i++)
{
scanf ("%d",&a[i]);
}
Example
Array Input and Show include<stdio.h> include<conio.h> void main() { int arr[5],i; clrscr(); for(i=0;i<5;i++) { printf("Enter a number "); scanf("%d",&arr[i]); } for(i=0;i<5;i++) { printf("%d\t",arr[i]); } getch(); }
Output 
Enter a number 1 Enter a number 3 Enter a number 5 Enter a number 6 Enter a number 7 1 3 5 6 7

Two Dimensional (2-D) Array

  • Matrixes in the C language represent two-dimensional arrays as rows and columns. This is also called an array of arrays or a list of arrays.

Declaration of 2D Array

  • It uses two subscripts.
  • The first subscript for the row size.
  • The second subscript is for the column size.
Syntax
data_type array_name [row size][column size];
Numbers of element in 2D are
Number of element = Row * Column
Example
int a[4][3];
Total number = 4*3=12
  • The following figure shows four rows and three columns in each row…….
000102
101112
202122
303132
  • As you can see, this Array is a 2-D Array that can store 12(4*3) Values of integer type, also known as a Matrix.

Initiation of the 2D array

  • Initializing a 2D array requires the following:
    • Column size is a must.
    • Row size is optional.
  • This memory contains the following values:
123
456
789
101112
Memory Allocation
  • Characters are represented in single quotes otherwise they are treated as variables.
  • As soon as the array is filled with values less than its dimensions, the remaining will be set to 0.

Element Access for a 2D Array

  • It’s similar to accessing the elements of a 1-d array, but in a 2-d array, there will be two indexes for each element.
where, 
First, will indicate row number. The second will indicate column number.
Example
              Sum of Matrix
include<stdio.h>
include<conio.h>
void main() 
{
  int i,j,a[5][5],n,m,b[5][5];
  clrscr();
  printf("Enter Number of Row in First Array:");
  scanf("%d",&n);
  printf("Enter Number of Column in First Array:");
  scanf("%d",&m);
  printf("Total Number of Value to Enter (Row*Column):%d\n",n*m);
  for(i=0;i<n;i++)
  {
    for(j=0;j<m;j++)
     {
       scanf("%d",&a[i][j]);
     }
  }
  for(i=0;i<n;i++)
  {
    for(j=0;j<m;j++)
     {
       printf(" %d ",a[i][j]);
     }
    printf("\n");
  }
  printf("Enter Number of Row in Second Array:");
  scanf("%d",&n);
  printf("Enter Number of Column in Second Array:");
  scanf("%d",&m);
  printf("Total Number of Value to Enter (Row*Column):%d\n",n*m);
  for(i=0;i<n;i++)
  {
   for(j=0;j<m;j++)
    {
      scanf("%d",&b[i][j]);
    }
 }
 for(i=0;i<n;i++)
 {
   for(j=0;j<m;j++)
    {
      printf(" %d ",b[i][j]);
    }
   printf("\n");
 }
 printf("Sum of Matrix:\n");
 for(i=0;i<n;i++)
 {
   for(j=0;j<m;j++)
    {
      printf(" %d ",a[i][j]+b[i][j]);
    }
   printf("\n");
 }
 getch();
}
Output
Enter Number of Row in First Array:2 Enter Number of Column in First Array:2 Total Number of Value to Enter (Row*Column):4 5 6 7 8 5 6 7 8 Enter Number of Row in Second Array:2 Enter Number of Column in Second Array:2 Total Number of Value to Enter (Row*Column):4 1 2 3 4 1 2 3 4 Sum of Matrix: 6 8 10 12

Multi-Dimensional Array

  • We can use the dimension based on our requirements.
  • In the n-Dimension Array, there is a need for n square brackets and n subscript.
  • They are rarely used.
Syntax
data_type array_name [size][size][size]…..;
Example
int a[3][4][2];

Declaration of Multi-Dimensional Array

int a[3][4][2]= {
{
{4,2},
{5,3},
{7,8},
{6,3},
},
{
{1,2},
{4,5},
{7,3},
{6,4},
},
{
{4,6},
{9,2},
{7,6},
{8,4}
}
};
Example 
include<stdio.h> include<conio.h> void main() { int i, j, k; int arr[3][3][3] = { { {11, 12, 13}, {14, 15, 16}, {17, 18, 19} }, { {21, 22, 23}, {24, 25, 26}, {27, 28, 29} }, { {31, 32, 33}, {34, 35, 36}, {37, 38, 39} } }; clrscr(); printf(":::3D Array Elements:::\n\n"); for(i=0;i<3;i++) { for(j=0;j<3;j++) { for(k=0;k<3;k++) { printf("%d\t",arr[i][j][k]); } printf("\n"); } printf("\n"); } getch(); }
Output
:::3D Array Elements:::
11 12 13
14 15 16
17 18 19

21 22 23
24 25 26
27 28 29

31 32 33
34 35 36
37 38 39

Conclusion

I hope you have learned how to use arrays in C. Click on the following topics to learn more about this language: