Arrays and Structures of the same type sequentially in memory. Therefore, an integer array holds a sequence of integers; a character array holds a sequence of characters, and so on. The size of the array is referred to as its dimension. .
Types of Arrays: There are different types of arrays used in C++. They are: • One-dimensional arrays • Two-dimensional arrays • Multi-dimensional arrays . . One-dimensional array This is the simplest form of an array.
A one dimensional array represents values that are stored in a single row or in a single column. Declaration Syntax: <data type>< > [< >]; declares the basic type of the array, which is the type of each element in the array. specifies the name with which the array will be referenced. defines how many elements the array will hold.
Size should be specified with square brackets [ ]. Example: int num[ ]; Chapter Page - - In the above declaration, an array named “num” is declared with elements (memory space to store different values) as integer type. For the above declaration, the compiler allocates memory locations (boxes) referenced by a common name “num” as given below int num[ ]; subscripts Each element (Memory box) has a unique index number starting from which is known as “subscript”. The subscript always starts with and it should be an unsigned integer value.
Each element of an array is referred by its name with subscript index within the square bracket. For example, num[ ] refers to the 4th element in the array. Some more array declarations with various data types: char [ ]; // character array named with size float salary[ ]; // floating-point array named salary with size int a[ ], b[ ], c[ ]; // multiple arrays are declared of type int Memory representation of an one dimensional array The amount of storage required to hold an array is directly related with type and size. The following figure shows the memory allocation of an array with five elements.