Arrays in C ( Introduction to Arrays)
What are arrays and why did they come in picture ?
Suppose, we need to see the average temperature observed in a month: Ideally, you create 30/31 variables; scan each variables value, take sum and then average it out.
Now consider if you have to average out the temperatures over 6 months or a year or more. It sounds crazy right , to scan 365 or more values , operate on them and give the result ? Also, in practical applications, the data involved is huge.
Arrays come to our rescue !
Arrays are nothing but a collection of items of the same data type having fixed size.
- It is a collection of similar data elements.
- These elements are stored in consecutive or contiguous blocks of memory.
- It is a derived data type.
- The memory locations where these items are stored are referenced to by index or subscript. Index is an ordinal number of the element beginning from the starting location of array.
- Array is referenced by a single name or variable.
- Array representation in memory is as shown below: Consider an array of temp (temp standing for temperatures)
- Above is an array of 5 elements, with index starting from 0 to 4.
Declaration of Array:
As we have seen that every variable needs to be declared before usage, similarly arrays in C also need to be declared.
Declaring an array has the following components:
- data_type:what kind of value will be stored. for e.g: marks are of int data type, percentage to be stored will be float data type , grade would be char data type etc.
- array_name: The name using which the array will be identified. e.g: marks, temp etc.
- array_size: the number of elements that the array would comprise of.
data_type array_name [array_size];
float temp ; // the statement declares an array called temp of float data type comprising of maximum 5 elements.
Here, the first element of the array would be denoted as temp , next element temp and so on and so forth.
Since indexing starts from 0, the nth element here 5, will be stored at n-1 th i.e 4th position.
int marks; // the statement declares an array called marks of integer data type comprising of maximum 20 elements. char grades ; // the statement declares a char array which can hold a total of 10 elements.
- Convenient way to store large amounts of similar data.
- It is used to represent multiple items of similar nature using a single name.
- It allows us to store data in multi dimensional arrays. ( We will lesrn about them in a later section.)
- It is used to create other data structures like heaps, linked lists etc.
- You need not access elements sequentially, random access is allowed.
- Since elements are stored in consecutive blocks hence they are useful to use in iterations.
- Since size of array is known during compile time, there is no chance of memory runout for arrays.
- Since, it is a static data structure that is has fixed size we should know or determine array size at compile time itself. No modifications can be done to array size during runtime.
- Inserting and deleting elements from array is a tedious task, as it would involve shifting of some or all the elements of the array which would also involve managing memory space for it as well.
- No element can be appended after the end of an array.
- Since we declare array at compile time itself with a particular size, it is possible that a lot of memory space might get wasted if only some address space is used and occupied.
- Operations like insertion, deletion are time consuming tasks on arrays.
Apart from being widely used in programming, arrays have additional applications as well:
- Used in mathematical problems like matrices etc.
- They are used in implementation of other data structures like linked lists etc.
- Database records are usually implemented as arrays.
- Used in lookup tables by computer.
- It effectively executes memory addressing logic wherein indices act as addresses to the one dimensional array of memory.