Arrays
Arrays
ONE-DIMENSIONAL ARRAY
INTRODUCTION TO ARRAY
A variable of fundamental data types, int, float, char, double, can store only one value at a time. Thus if it is required to store a set
of values, then we have to use a set of variable. But the storing, processing, and manipulation of a set of variables is not efficient. C
supports a derived data type, called Array, that can be used to handle large volume of data and facilitates efficient storing, processing,
and manipulation of data item.
DATA TYPES
Array is a fixed size collection of data items of the same data type. The individual data item of an array is known as element of an
array.Each element of an array share the same name but they are identified by a uniqueindex/subscript. Hence an array is also known as
Indexed/subscript variable.
ONE-DIMENSIONAL ARRAY :
In a 1D-Array, each element of an array is identified by a single subscript. The elements of the array are logically organized in a
single row of data items.
Declaration of 1D-Array :
Syntax:- datatypearrayname[size];
Datatype of an array specifies the datatype of each element of an array.
Size of an array specifies the maximumnumber of elements of an array. The size should always be an integer constant.
When an array is declared, the computer reserves memory location for all the elements of an array at the time of compilation. Hence
the size should be integer constant, but not a variable.
Example : When an array num is declared of size 10, as: int x[10];
The computer reserves 10 memory locations at the time of compilation as follows:
63
1050 1052 1054 1056 1058 1060 1062 1064 1066 1068
x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]
Thus each element of an array have the same name but they differ in their index/subscript. The index of the elements of the array
starts from 0 and index of last element of the array is size-1. The index/subscript of first element x[0]=0. The index/subscript of
second element x[1]=1.The index/subscript of the first element of an array is called lower bound. The index/subscript of the last
element of an array is called Upper bound.
In C, there is no check on bounds of an array. If for an array num[5] having valid elements num[0], num[1], num[2], num[3] and
num[4], we tries to access the elements beyond these subscript like num[–2], num[8], the compiler will generate no error message. Thus
the programmer should take care about the bound checking of an array.
But if the size of the array is not mentioned in the program, then storing data into array requires:
1. Inputting the Number of Elements of The Array
2. Inputting the Value of Each Element of the Array
INITIALIZATION OF 1D-ARRAY :
At the time of declaration of the array, we can assign an initial value to the elements of an array.
Syntax : datatype arrayname[size]={list of values};
If we are initializing an array, then it is optional to specify the size of an array. The compiler allocate memory space for all the
initialized elements.
Example : int x[ ]={ 10,20,30,40} /* Here size of the array become the no. of initial values. */
65
If the no. of initial values is less than the size of the array, then remaining elements are by default initialized to 0.
1050 1052 1054 1056 1058 1060 1062 1064 1066 1068
10 20 0 0 0 0 0 0 0 0
x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] x[8] x[9]
Important Tips:
i=3; In this case, element num[3]=80 and then i will become 4.
num[i++]=80;
i=3;
num[i]++; /* In this case, the element num[3] will increment by 1. */
i=0;
int num[4]={ 10,20,30,40};
p=num[i++] + num[++i] + num[i++]; /* p = num[1] + num[1] + num[1] = 20 + 20 +20 =60 */
printf(“%d\n”,p);
for(i=0; i<4; i++)
printf(“%d”, num[i]);
Output : 60 10 20 30 40
The base address of the array is the memory address of the first element (0 th index) of the array. All the elements of an array are
stored in contiguous memory locations. The compiler defines the name of the array as a constant pointer to the first element of the array .
Example : int x[5];
In the above example, x is the constant pointer to the x[0] element of array, i.e., x= &x[0].
To declare a pointer to an array, we can make pointer point to the x[0] elements by the following assignment:
int *ptr;
ptr = x;
(ptr + i) gives the address of x[i] element. In other words (ptr + i) = &x[i]
*(ptr + i) gives the value of x[i] element. In other words *(ptr + i) = x[i]
Sample Program: A Program to Compute the Sum of the Elements of Array Through Pointer
#include<stdio.h>
#include<conio.h>
void main()
{
int x[15];
int *ptr = x;
66
int i,n,sum=0;
/* Input The Elements of The Array */
printf("Enter the No. of Elements of Array ");
scanf("%d",&n);
printf(“\nEnter the Elements of the Array “);
for( i=0; i<n; i++)
{
scanf(“%d”, ptr);
ptr++;
}
ptr=x;
/* Compute the sum of the set of numbers */
for(i=0;i<n;i++)
{
sum=sum + *ptr;
ptr++;
}
printf("The sum of the elements = %d",sum);
}
The called function is defined with the array as formal argument as follow:
ReturnType FunctionName(int arrayname[], int size)
{
/* Body of the Function that can access the Input Array */
}
Sample Program: A Program to compute the sum of a set of numbers using function.
#include<stdio.h>
#include<conio.h>
/* Function Prototype */
void sum(int[],int);
void main()
{
int x[20];
/* Accept the Array */
printf("Enter the No. of Elements of the Array ");
scanf("%d",&n);
printf(“Enter the Elements of the Array “);
for(i=0;i<n;i++)
scanf("%d",&x[i]);
/* Call the function sum() */
sum(x,n);
getch();
}
67