Arrays are among the most fundamental data structures in programming, offering a simple yet efficient way to store and manage collections of elements of the same type. Whether you’re working with integers, characters, or floating-point numbers, arrays provide a structured method to organize data in contiguous memory locations. This makes accessing and manipulating data fast and predictable, which is especially valuable in performance-critical applications.
For example, an integer array like int a[] = {1, 2, 3, 4, 5};
allows storing and accessing multiple integers under a single variable name. Similarly, a character array char c[] = {'a', 'b', 'c', 'd', 'e'};
can handle sequences of characters efficiently.
1.Array is a data structure.
2.Array is a simple and fast data structure that stores same type of elements
Example:
int a[]={1,2,3,4,5}; integer array
char c[]={‘a’,’b’,’c’,’d’,’e’}; character array
There are different aspects in an array:
Memory representation of array:In array memory are stored in continguous memory location. It stores one after the other .
int a[5]
char c[6];
float f[5]; .
#include <stdio.h>
int main()
{
// declaring array of integers
int arr_int[5];
// declaring array of characters
char arr_char[5];
return 0;
}
Array initialization: ( assigning some value to the variable.)
datatype a [size] = {v1, v2, … vN};
arr[]={1,2,3,4,5};(compiler automatically deduce the size of the array.)
You can assign values to an array after declaring it by using a loop, like this:
Example program to different types of initialization:
#include<stdio.h>
int main()
{
int a[5]={1,2,3,4,5,6,7,8}; // with declaring the size
int b[]={1,2,3,4,5,6,7,8}; // without declaring the size
int c[] //without size and initializer list
for(int i=0;i<5;i++)
{
c[i]=i*2;
}
}
we can access any element of an array in c using the array subscript operator [ ] and the index value i of the element.
Simple example:
#include<stdio.h>
int main()
{
int a={1,2,3,4,5,6};
printf(“%d”,a[1]);
printf(“%d”,a[2]);
return 0;
}
#include<stdio.h>
int main()
{
int a={1,2,3,4,5,6};
for(int i=0;i<5;i++)
{
printf(“%d”,i)
}
return 0;
}
// C Program to illustrate element access using array
// subscript
#include <stdio.h>
int main()
{
// array declaration and initialization
int arr[5] = { 15, 25, 35, 45, 55 };
// accessing element at index 2 i.e 3rd element
printf(“Element at arr[2]: %d\n”, arr[2]);
// accessing element at index 4 i.e last element
printf(“Element at arr[4]: %d\n”, arr[4]);
// accessing element at index 0 i.e first element
printf(“Element at arr[0]: %d”, arr[0]);
return 0;
}
1.one dimensional array:
The one dimensional array also known as 1-D array it has only single row.
// C Program to illustrate the use of 1D array
#include <stdio.h>
int main()
{
// 1d array declaration
int arr[5];
// 1d array initialization using for loop
for (int i = 0; i < 5; i++) {
arr[i] = i*2;
}
printf(“Elements of Array: “);
// printing 1d array by traversing using for loop
for (int i = 0; i < 5; i++) {
printf(“%d “, arr[i]);
}
return 0;
}
2.Two dimensional array:
It has both rows and columns
For Example: If we are storing in single array it is very difficult to store it requires a lot boxes but if we are two dimensional array we can stores in a single box.storing 5 rows of mangoes and 5 columns of apples then we use two dimensional array (in a single box);
Syntax to declare two dimensional array:
A[size][size];
Program for the two dimensional array:
Program:
#include<stdio.h>
int main()
{
int a[3][3];
for (int i = 0; i <=2; i++) {
for (int j= 0; j <= 2; j++) {
scanf(“%d”,&a[i][j]);
} //input the element
}
for (int p = 0; p <= 2;p++) {
for (int m= 0; m <=2; m++) {
printf(“%d”,a[p][m]);
}
}
}
Disadvantages of array:
Indian Institute of Embedded Systems – IIES