An Array in C is a collection of items. an array is a collection of same data type (arrays in c).
An array Data structure or Arrays is a linear type of data structure. It is used to hold a collection of the element.

What is Array in C?

Array Definition: Array is a set of similar type of data. Array only hold a homogeneous collection of data.
An array stored in a systematic order and sequential index. Array index starts with zero
It stores a homogeneous collection of elements that is it stores data of the same data. an array is derived data type.

Why array used in c?

Suppose if we want to store 10 value of integer type instead of using 10 different variables for storing 10 value we use a single variable to store 10 value in one array of integer type. Array is used to hold multiple values in a single variable.

Example: –

Suppose we have 5 numbers (similar type integers) are stored in array1.

Array in C
Array in C

As we see array1 making array for 5 elements and the index value of an element in an array will be from 0 to 4 is started continuously and its index starts with 0.

Why does the array index start with 0?

The array index in C starts with 0 because in C the name of an array is a pointer, which is a reference to a memory location. Therefore, an expression *(arr + n) or arr[n] locates an element n-locations away from the starting location because the index is used as an offset. Likewise, the first element of the array is exactly contained by the memory location that array refers (0 elements away), so it should be denoted as *(arr + 0) or *(arr) or arr[0].

Let us see the array in C in detail
In another way, Array holds a collection of data in one single common name.
Before using array we need to declare the array.

How to declare array?

Array Declaration: To declare array we need to specify the type of data we want to store, the name and size of the array.
Syntax for array declaration:
Data_type array_name[Areay_size];
Example :
To store 5 integer numbers
int num[5];

Declare array
Array

Note:- Once we declare an array then size and type of array cannot be changed.

How to initialize an array?

Array Initialization: This operation assigns a value to an array. It is also known as array initialization.
Syntax to initialize the array:
Array_name[0]=value;
In this way, we can assign value to each array using its index.
Example:-
num[0]=10;
num[1]=20;
num[2]=30;
num[3]=40;
num[4]=50;

There is a another way to initialize an array
You can initialize array while you are declaring an array.
The syntax for Array initialization
You can specify size and initialize the array with values.
Data_type array_name[size]={value1,value2..valueN};
Or
Data_type array_name[]={value1,value2..valueN};
In this syntax it not contains size. Compiler considers size as we initialized array with specific numbers.
Example:-
int num[5]={10,20,30,40,50};
Or
int num[]={10,20,30,40,50};
Here we haven’t specified size. Still compiler considers size 5 as we initialized array with 5 numbers.

Accessing an array element

The array has its index. We can access an array element by its index.
Syntax to access array element
Array_name[index]
Example
int num[]={10,20,30,40,50};
var=num[0]

Access elements out of its bound!
Suppose we declared the array with 5 numbers
int num[]={10,20,30,40,50};
We can access any element within index 0 to 4. If we try to access element other than this then we will get an unexpected error.
example: num[6]; // Unexpected array

Types of Array

There are different types of the array in C which are as follow:

  1. One dimensional Array
  2. Multidimensional Array

One dimensional array

It is also known as a single-dimensional array. One dimensional array is a linear type of array.

One dimensional array
One dimensional array

Syntax to declare a one-dimensional array
data_type array_name[size];
One dimensional array example
int num=[5]={1,2,3,4,5};

One dimensional array programs in c
#include <stdio.h>

int main()
{
int i; //Array declaration
printf("Array values\n");

int arr[5]={1,2,3,4,5}; //Array initialization

for(i=0;i<5;i++)

{

printf("%d\n",arr[i]);

}

return 0;
}

Output:-

One dimensional array

Multi dimensional array

You can create an array of arrays is known as a multidimensional array. we create a two-dimensional array or we can create a three-dimensional array.

Two-dimensional array

The two-dimensional array is also known as a 2D array or matrix. It is an array of array. A matrix can be represented as a collection of rows and columns.
Syntax to declare two-dimensional array
data_type array_name[rows][cols];
Two-dimensional array example
int Num[2][4];
Initialize two-dimensional array
int num={ {1,2,3,4}, {5,6,7,8} };
The num is a two-dimensional array which holds 8 values. The array is table consist of 2 rows and 4 columns.

Two dimensional array
Two-dimensional array
Two-dimensional array program in c
#include <stdio.h>

int main()
{
int i,j;

int a[3][3]={ {1,2,3},{4,5,6},{7,8,9} };

printf("Two dimensinonal array \n");
for (i=0; i<3;i++)

{

for(j=0; j<3; j++)

{

printf ("%d\t",a[i][j]);

}
printf("\n");

}

}

Output: –

Two dimensional array

Three-dimensional array

As we created two-dimensional array similarly we can create a three-dimensional array.
Syntax to declare three dimensional array
data_type array_name[][][];
Example:
int num[2][4][3];
The above num will store 24 values.
Initialization of three dimensional array
you can initialize a three dimensional array in a similar way like a two dimensional array.
int num[2][4][3]={ {{ {3,4,5,6}, {4,5,6,1}, {22,4,2,7}}, {{9,7,8,44}, {4,6,5,6}, {3,3,2,2}}};

The operation performed on an array

  1. Adding element
  2. Deleting element
  3. Sorting element
  4. Searching element
  5. Re-arranging element
  6. For performing a matrix operation

Application of array in c

  • Sorting Elements
  • CPU Scheduling
  • Recursive Function

Advantages of array

  • Collection of a homogeneous element.
  • It has an index.
  • We can access any element by index.
  • To implement data structure like a stack, queue.

Disadvantages of array

  • We must know how many elements to store.
  • An array has a fixed size that is memory allocated to an array cannot be increased or reduced.
  • If we allocated more memory then we are using then memory will be wasted.
  • It is stored in a consecutive memory location so insertion and deletion take more time.

 

 

Write A Comment