In this post we will be talking about array, their usage and importance in C.
Array
Array in simple world is a collection/container of elements of same type. Array of any type is possible but the elements in array cannot be of multiple types.
- We can have array of int type.
- But not a array with 5 element of int and other element of other types .
Why Array?
Imagine a problem where you want to input the marks of 10 student in a subject from the user and display the marks in descending number.
Our major problem above will be to store the number input by the user.
Here we have two options :
- Either construct 10 variables for storing marks of 10 student .(A trash Idea)
- Or Construct a container (Array of integers in this case) which can store all the marks of student .
First Glance of an Array
As mentioned earlier array is a nothing else than a collection for us.
int marks[10] = {'1','2','3','4','5'};
This is what an array looks like ( A declaration of array).
Here int refers to the type of element the array is storing.
marks is the name given to our collection(array). It is an collection of integers marks.Let's get bit more technical here It is an array of marks in integer.
We can also see [10] .This is what we call index/subscript/dimension of an array. It indicates the maximum number of element of that can be stored in our array.
Declaration and initialization of Array
We have already seen how an array can be initialized however there are other ways too.
int score[10]; //array of ints
char name[20]; //array of chars
int marks[] = {'1','2','3','4','5'};
Something has changed? Our third array doesn't have any index on it . Yeah that's possible but only when we initialize the members of array.
int marks[]; //error
The above declaration is invalid. The compiler needs to know the dimension of array for some reason which we will discuss later.
Accessing Array Elements
As we have learnt about initializing and declaring an array .Let us see how can we access the elements of an array.
Before this let's learn a very basic but an important thing about array.
Arrays always start with 0.
What it mean is The first element of an array is in Zero position,the second on first and so on.
Okay let's now access our array elements.
int marks[] = {'1','2','3','4','5'};
We should always keep in mind that an Array always starts from 0.
marks[0]; //equal to 1
marks[1];//equal to 2
marks[2];//equal to 3
marks[3]; //equal to 4
So let's write a program where we'll implement what we have learnt so far.
A program to Print out all the elements of array.
#include<stdio.h>
int main()
{
int store[5]={1,2,3,4,5};
printf("The numbers are \n"); for(int i = 0; i < 5 ; i++)
{
printf("%d ",store[i]);
}
return 0;
}
Here inside our main function we have initialized an integer array store which can store maximum of 5 integers.
As we already learnt that
store[0] refers to the first element of array. (1 in this case)
store[4] refers to the last element of the array.(5 in this case) So the output will be
1 2 3 4 5
We are using the similar concept but using the for loop .
When i =0 store[0] = 1
i=1 store[1] = 2
and soon.You know a for loop well .I believe.
Entering Data into an Array
Entering data into an array is as similar as entering a value into our variables .
Let's grow our above program in such a way that it asks the user to input 5 numbers which we'll store in an array and display them.
#include<stdio.h>
int main()
{
int store[5];
for(int i =0;i<5;i++)
{
printf("Enter the element no %d ",i+1);
scanf("%d",&store[i]);
}
printf("The numbers are \n");
for(int i = 0; i < 5 ; i++)
{
printf("%d ",store[i]);
}
return 0;
}
The output will be
Enter the element no 1 1
Enter the element no 2 2
Enter the element no 3 3
Enter the element no 4 4
Enter the element no 5 5
The numbers are
1 2 3 4 5
Here we used the &(address of operator) to store the value.
Memory Allocation in Array
Let's now learn whats going behind the scene with arrays.
Whenever we make an array declaration like:
float arr[5]; //array of float
20 (4 byte for a float so 20 byte for 5 int)bytes of memory will be immediately reserved for us.
Although the array is not being initialized garbage value or 0 (depends on the storage class or scope of declaration)is stored in that particular memory
Here 1024 ,1028 are imaginary memory address of the variable but the allocated memory address is always contiguous (continuous) .We can clearly see our first element is at address 1024 while second element at 1028 ,third at 1032 and so on .
The contiguous memory location of an array can be demonstrated with the following program below.
#include<stdio.h>
int main()
{
int store[5];
for(int i =0;i<5;i++)
{
printf("Enter the element no %d ",i+1);
scanf("%d",&store[i]);
}
printf("The numbers are \n");
for(int i = 0; i < 5 ; i++)
{
printf("The memory address of store[%d]\n
is %u and value at %u is %d\n",
i,&store[i],&store[i],store[i]);
}
return 0;
}
The output is
Enter the element no 1 1
Enter the element no 2 2
Enter the element no 3 3
Enter the element no 4 4
Enter the element no 5 5
The numbers are
The memory address of store[0]
is 6422276 and value at 6422276 is 1
The memory address of store[1]
is 6422280 and value at 6422280 is 2
The memory address of store[2]
is 6422284 and value at 6422284 is 3
The memory address of store[3]
is 6422288 and value at 6422288 is 4
The memory address of store[4]
is 6422292 and value at 6422292 is 5
👌👌
ReplyDelete