What is an array?
An array is a collection of variables of the same type that are referred to through a common name. A specific array element is accessed by an index. In C++ arrays consist of contiguous memory location. The lowest memory address corresponds to first element and the highest to the last element of the array. Arrays could be single dimensional or multi dimensional. A string is most common example of an array. It is a single dimensional array of characters.
- The general form for declaring a single dimensional array is :
type var_name[size];
type is data type
var_name is name given to the array variable
size is size of the array;
Example : int a[10]; declares an array of 10 integers.
- Array indexing starts from 0. To access nth element of an array (n-1)th index is to be used. i.e. a[3] = 5; will assign value 5 to the 4th element of array.