Arrays
Arrays
Introduction
A data structure is the way data is stored in the machine and the functions used to access that
data. An easy way to think of a data structure is a collection of related data items. An array is a
data structure that is a collection of variables of one type that are accessed through a common
name. Each element of an array is given a number by which we can access that element which is
called an index. It solves the problem of storing a large number of values and manipulating them.
Arrays :
Previously we use variables to store the values. To use the variables we have to declare the
variable and initialize the variable i.e, assign the value to the variable. Suppose there are 1000
variables are present, so it is a tedious process to declare and initialize each and every variable
and also to handle 1000 variables. To overcome this situation we use the concept of array .In an
Array values of same type are stored. An array is a group of memory locations related by the fact
that they all have the same name and same type. To refer to a particular location or element in
the array we specify the name to the array and position number of particular element in the array.
Array in C is one of the most used data structures in C programming. It is a simple and fast way
of storing multiple values under a single name. In this , we will study the different aspects of
array in C language such as array declaration, definition, initialization, types of arrays, array
syntax, advantages and disadvantages, and many more.
What is Array in C?
The One-dimensional arrays, also known as 1-D arrays in C are those arrays that
have only one dimension.
Declaration:
Before using the array in the program it must be declared
Syntax:
data_type array_name[size];
data_type represents the type of elements present in the array. array_name represents the name of
the array.
Size represents the number of elements that can be stored in the array.
Example:
int age[100];
float sal[15];
char grade[20];
Here age is an integer type array, which can store 100 elements of integer type. The array sal is
floating type array of size 15, can hold float values. Grade is a character type array which holds
20 characters.
The C arrays are static in nature, i.e., they are allocated memory at the compile time
Initialization:
We can explicitly initialize arrays at the time of declaration.
Syntax:
Value1, value2, valueN are the constant values known as initializers, which are assigned to the
array elements one after another.
Example:
int marks[5]={10,2,0,23,4};
NOTE:
1. In 1-D arrays it is optional to specify the size of the array. If size is omitted during
initialization then the compiler assumes the size of array equal to the number of initializers.
Example:
int marks[]={10,2,0,23,4};
2. We can’t copy the elements of one array to another array by simply assigning it.
Example:
int a[5]={9,8,7,6,5};
int b[5];
array_name [index];
One thing to note is that the indexing in the array always starts with 0, i.e., the first element is at
index 0 and the last element is at N – 1 where N is the number of elements in the array.
// subscript
#include <stdio.h>
int main()
return 0;
Output:
Element at arr[2]: 35
Element at arr[4]: 55
Element at arr[0]: 15
For processing arrays we mostly use for loop. The total no. of passes is equal to the no. of
elements present in the array and in each pass one element is processed.
Example:
#include<stdio.h>
main()
int a[3],i;
scanf(“%d”,&a[i]);
printf(“%d”,a[i]);
printf(“\n”);
We can update the value of an element at the given index i in a similar way to accessing an
element by using the array subscript operator [ ] and assignment operator =.
array_name[i] = new_value;
C Array Traversal:
Traversal is the process in which we visit every element of the data structure. For C array
traversal, we use loops to iterate through each element of the array.
array_name[i];