Arrays in C language provide a mechanism for declaring and accessing several data items with only one identifier, thereby simplifying the task of data management.
Before we learn about Arrays, go through this – C language provides four basic data types int, char, float and double. These basic data types are very useful, but they can handle only a limited amount of data.
As programs become larger and more complicated, it becomes increasingly difficult to manage the data. Variable names typically become longer to ensure their uniqueness. And, the number of variable names makes it difficult for the programmer to concentrate on the more important task of correct coding.
Arrays provide a mechanism for declaring and accessing several data items with only one identifier, thereby simplifying the task of data management.
Many programs require the processing of multiple, related data items that have common characteristics like list of numbers, marks in a course, or enrolment numbers. This could be done by creating several individual variables. But this is a hard and tedious process.
For example, suppose you want to read in five numbers and print them out in reverse order. You could do it the hard way as:
scanf(“%d %d %d %d %d”,&amp;amp;a1,&amp;amp;a2,&amp;amp;a3,&amp;amp;a4,&amp;amp;a5);
printf(“%d %d %d %d %d”’,a5,a4,a3,a2,a1);
Does it look good if the problem is to read in 100 or more related data items and print them in reverse order? Of course, the solution is the use of the regular variable names a1, a2 and so on. But to remember each and every variable and perform the operations on the variables is not only tedious a job and disadvantageous too. One common organizing technique is to use arrays in such situations.
An array is a collection of similar kind of data elements stored in adjacent memory locations and are referred to by a single array-name. In the case of C, you have to declare and define array before it can be used.
Declaration and definition tell the compiler the name of the array, the type of each element, and the size or number of elements.
To explain it, let us consider to store marks of five students.
They can be stored using five variables as follows: int ar1, ar2, ar3, ar4, ar5;
Now, if we want to do the same thing for 100 students in a class then one will find it difficult to handle 100 variables. This can be obtained by using an array.
An array declaration uses its size in [ ] brackets. For above example, we can define an array as:
int ar ;
where ar is defined as an array of size 100 to store marks of integer data-type. Each element of this collection is called an array-element and an integer value called the subscript is used to denote individual elements of the array.
An ar array is the collection of 200 consecutive memory locations referred as below:
Representation of an array
In the above figure, as each integer value occupies 2 bytes, 200 bytes were allocated in the memory.
These tutorials explain the use of arrays, types of arrays, declaration and initialization with the help of examples. We also discuss subscript, how to process the arrays and multi-dimensional arrays.