Q : What are merits and demerits of array in C?
Why we need an array.
We need array when there is a need to process more than data with similar type. For example
- Taking marks of 10 student needs of ten integer
- Taking name of more than one character
- Structure record and many more
Criteria mentioned above can also be achieved through variable , but variables have following drawback which lead to use array instead of Variable.
- Each variable must has unique name which increase complexity when a large number of variables need to make.
- Each variable load in memory at random order. So if you want to process all variable then you need to process each variable individually because allocation of space is not continuous.
For understand the above point you need to imagine a real life example.
Two close friend group decided to watch a movie and they follow two different approaches as follows
- Friends in Group one purchase ticket individually without any coordination between them for seating arrangement
- Friend in group two book seats continuously i.e. seats no 1 to 10.
So Group two can make better communication than group one. In same way array works.
- Continuous memory allocation.
- Process a collection of similar data type.
- Can be used to make data structure like stack,tree,queue etc.
- Easy to visit each element using index.
- Static memory allocation
- Due to static memory allocation number of element cannot be increase nor decrease at run time.
- The elements of array are stored in consecutive memory locations. So insertions and deletions are very difficult and time consuming.