- An array is a collection of data of the same type stored in a contiguous memory location.
- The array is the derived data type in the C programming language which can store the primitive data types, such as pointers structure, etc.
Advantages:
1) Code Optimization: Less code to access the data.
2) Ease of traversing: By using the for loop, we can retrieve the elements of an array easily.
3) Ease of sorting: To sort the elements of the array, we need a few lines of code only.
4) Random Access: We can access any element randomly using the array.
Disadvantages:
1) Fixed Size: Whatever size, we define at the time of declaration of the array, we can’t exceed the limit. So, it doesn’t grow the size dynamically like Linked List which we will learn later.
Properties of Arrays:
- Each element of an array is of the same data type and carries the same size.
- Elements of an array are stored in contiguous memory locations where the first element is stored at the smallest memory location
- Elements of the array can be randomly accessed since we can calculate the address of each element of the array with the given base address and the size of the data element.
Array Declaration:
syntax:
data_type array_name [arraySize];
int a[5];
int num[5] = {1,2,3,4,5};
we can also initialize an array like this:
int num[] = {1,2,3,4,5};
- once the array is declared, then the individual elements in the array can be referred to by the number in the brackets following the array name this number specifies the position of the element in the array
- Subscript starts with 0, which means
arr[0]
represents the first element in the array arr. - Thus num[2] is not the second element but this is considered as the third element of the array.
For example:
int mydata[20];
mydata[0] /* first element of array mydata*/
mydata[19] /* last (20th) element of array mydata*/
One-dimensional array:
One dimensional array is an array that has only one subscript specification that is needed to specify a particular element of an array. A one-dimensional array is a structured collection of components (often called array elements) that can be accessed individually by specifying the position of a component with a single index value.
Syntax:
data-type arr_name[array_size];
Entering data into an array
for(i=0;i<=4;i++)
{
printf("Enter number");
scanf("%d",&num[i]);
}
}
Here we are iterating the array from 0 to 4 because the size of the array is 5, and all the values will be stored in the corresponding array elements using scanf function.
Reading data from an Array
Suppose we have to display the above entered elements
for(i=0;i<4;i++)
{
printf("%d",num[i]);
}
*Program to find the sum of array elements expand by the user*/
#include<stdio.h>
void main()
{
int sum=0,i;
int num[5]; /*array declaration*/
for(i=0;i<=4;i++)
{
printf("\nEnter number");
scanf("%d",&num[i]); /*Storing data in array*/
}
for(i=0;i<=4;i++)
sum=sum+num[i];
}
printf("Ssum of numbers=%d",sum);
getch();
}
*Passing array to function using call by reference
#including<stdio.h>
void display(int a);
void main()
{
int i;
int num[]={2,4,6,8,10}
for(i=0;i<=4;i++)
{
display(&num[i]);
}
}
void display(int*m)
{
printf("%d",*m);
getch();
}
Output=2,4,6,8,10
Passing an array to a function
Just like variable array can also be passed to a function as an argument
Two methods of passing array in function
- By call by value
- By call by reference, method