
Array Data Structure
An Array is a data structure that stores a collection of elements of the same data type. The elements of an array are stored in contiguous memory locations, and each element can be accessed using an index. The index of the first element in an array is 0, and the index of the last element is one less than the size of the array.
Arrays are used to store data in a way that makes it easy to access and manipulate the data.

Why Array Data Structure ?
Arrays are useful when we need to store a collection of values of the same data type. They allow us to access individual elements quickly and easily, and to perform operations on entire collections of data in a single operation. Arrays are widely used in programming for a variety of applications, such as sorting, searching, and data manipulation.
Advantages and Disadvantages of Array
Advantages of Array
- Arrays provide fast and efficient access to individual elements.
- They allow us to perform operations on entire collections of data in a single operation.
- Arrays are widely supported in programming languages and are easy to use.
Disadvantages of Array
- Arrays have a fixed size, which means that we cannot add or remove elements easily.
- They require contiguous memory allocation, which can be a limitation in some situations.
- Array elements must be of the same data type.
How to Declare an Array
To declare an array in most programming languages, we specify the type of data that the array will contain, followed by its name, and then the size of the array in brackets. For example, to declare an array of integers in C with a size of 10, we can use the following syntax:
int myArray[10];
This declares an array called myArray that can hold 10 integers. The size of the array must be specified when the array is declared, and it cannot be changed later.
How to Initialize an Array
We can initialize an array at the time of declaration by specifying a list of values in curly braces. For example, to initialize an array of integers with the values 1, 2, 3, 4, and 5 in C, we can use the following syntax:
int myArray[] = {1, 2, 3, 4, 5};
This initializes an array called myArray with the values 1, 2, 3, 4, and 5. The size of the array is automatically set to the number of values in the list.
How to Access Array Elements
We can access individual elements of an array by specifying their index or position in the array. In most programming languages, the index of the first element in an array is 0. For example, to access the second element of an array of integers called myArray in C, we can use the following syntax:
int secondElement = myArray[1];
This retrieves the second element of the myArray array and assigns it to a variable called secondElement. The index of the element we want to access is enclosed in square brackets.
Array Program in C
Here is an example program that demonstrates how to declare, initialize, and access elements of an array in C.
#include <stdio.h> int main() { // Declare and initialize an array of integers int myArray[] = {5, 10, 15, 20, 25}; // Access individual elements of the array // and print them to the console printf("First element: %d\n", myArray[0]); printf("Second element: %d\n", myArray[1]); printf("Third element: %d\n", myArray[2]); printf("Fourth element: %d\n", myArray[3]); printf("Fifth element: %d\n", myArray[4]); return 0; }Output
First element: 5 Second element: 10 Third element: 15 Fourth element: 20 Fifth element: 25
In this example, we declare an array of integers called myArray with five elements and initialize it with the values 5, 10, 15, 20, and 25. We then access each element of the array using its index and print it to the console using printf.