Concept of Array in C
In C programming language, a derived data type which can store a collection of elements of the same data type is possible. Such a derived data type is called an Array. The data type of Array elements can be either primitive or user-defined. An array in C programing language can be defined as a name given to number of consecutive memory locations, each of which can store the data of same data type. Thus, one can visualize an array as a collection of variables of the same data type. All the individual variables (also called elements) of Array can be referenced through the same Array name.
However to make distinction between individual elements an index is used to refer to a particular element in an array.
The general syntax for declaring an array is as follows:
<data-type> <Array-name> [<Element-Count>];
This means to declare an array of integers with 20 elements the valid C statements will be
int myArrayName [20];
where myArrayName is the name of the Array. It should be noted that this will declare an integer array of size 20. Because the elements of an Array are placed at consecutive memory locations, every element can be uniquely accessed using the Array name and its index/location within the Array. The general syntax to name any array element is as follows:
<Array-name> [<index>]
where index can have any value between 0 to ArraySize -1. It should be noted that, the lowest address corresponds to the first element and the highest address to the last element. This means the first element of an integer array declared previously can be accessed by myArrayName[0] and last element by myArrayName[19]. All other elements will have index between 0 and 19. After naming an array element, it can be used in any C expression wherein any other variable of same data type can be used.
An array is a collective name given to a group of similar quantities. These similar quantities could be percentage marks of 100 students, number of chairs in home, or salaries of 300 employees or ages of 25 students. Thus an array is a collection of similar elements. These similar elements could be all integers or all floats or all characters etc. but mixture of different data types. All elements of any given array must be of the same type i.e we can’t have an array of 10 numbers, of which 5 are ints and 5 are floats. Usually, the array of characters is called a “string”.
Arrays are of two types; one dimension array and multi-dimension array.