Definition of an Array
An Array is a collection of homogeneous data elements of a specific data type (i.e. int or float or char) that have been given a single name, which are stored at contiguous memory locations (i.e. one after the other).
Each individual element of an Array is referenced by a subscripted variable. The subscript or an index is enclosed in brackets after Array name.
Example:
int s [40];
- int is the data type.
- s is the name of the array.
- [40] is subscript or index.
If one subscript is used to refer to an element of an array the array is known as one-dimensional or linear array. If two subscripts are used to refer to an element of an array the array is known as two-dimensional array or matrix and so on.
The Arrays, which has two or more subscripts are known as multidimensional array. For each subscript, we use a pair of brackets.
Free Hand-Written Notes
Example:
int a [5] [6]; // Two Dimensional Array because of two subscripts
- [5] is the subscript 1.
- [6] is the subscript 2.
Declaration of Array
- Before using array in a program, it must be declared first like other variables.
- To declare an Array, we must provide the following information.
- Data Type of an Array
- The name of the Array (rules are same as variables name)
- The number of subscripts in an Array.
- The maximum value of each subscript.
- So the general form or syntax of array declaration is:
Storage_sp type name [sub1] [sub2]....;
Where storage_sp is Storage class specifier and is optional
type is the data type (int, char, or float)
name is the name of the array
sub1, sub2,… are number of subscripts available in an array.
Which are used to specify the dimensions of an array.
Example:
int s [40]; //One Subscript so one-dimension Array
- Where int specifies the type of the variable.
- s is the name of the array variable
- [40] is the subscript to specify that there are maximum.
- 40 elements stored in this array.
Note: The elements in an array start with 0 not one ends with one less than value of subscript.