C++ Arrays

Array in C++ is a collection of fixed size data belongings to the same data type. An array is a data structure provided in C++ which can store a number of variables of same data type in sequence. These similar elements could be of type int, float, double, char etc. All array elements are stored in the contiguous memory locations.

For Example :
int score[100]; /* Integer array of size 100 */
char name[10]; /* character array of size 10 */
Suppose, you want to store marks of 100 students. You can store marks of all students by creating 100 variable of int data type individually like studentMarks1, studentMarks2, studentMarks3 .... studentMarks100. This is very tedious, time consuming and difficult to maintain method of storing 100 marks.

C++ programming language provides support for array data structure, which solves the problem of storing N similar data. We can declare an integer array of length 100 to store marks of 100 students.

    int studentMarks[100];

In the above array declaration, we declared an array of name "studentMarks" which can store maximum of 100 integer data in contiguous memory locations. We can access the individual elements of an array using their index.
  • studentMarks[0] refers to the first element of array.
  • studentMarks[1] refers to the second element of array.
  • studentMarks[99] refers to the last element of array which is 100th element.

Suppose, we have an integer array of size 7 whose name is score. The base address or starting address of array score is 1000(base address is same as address of first element score[0]) and the size of int be 2 bytes. Then, the address of second element(score[1]) will be 1002, address of third element(score[2]) will be 1004 and so on.

C++ Array Memory Map
Points to Remember about Arrays in C++
  • An array is a collection of variables of same data types.
  • The size of array must be a constant integral value.
  • Array is a random access data structure. you can access any element of array in just one statement.
  • All elements of array are stored in the contiguous memory locations.
  • * Individual elements in an array can be accessed by the name of the array and an integer enclosed in square bracket called subscript/index variable like employeeSalary[5].
  • The first element in an array is at index 0, whereas the last element is at index (size_of_array - 1)

Advantage of Arrays in C++
  • Random Access : We can access any elements of array in O(1) time complexity.
  • Less amount of code : Using array we can aggregate N variables of same data type in a single data structure. Otherwise we have to declare N individual variables.
  • Easy access of elements : We can access any element of array using array name and index. We can access all elements serially by iterating from index 0 to size-1 using a loop.
  • Easy to implement algorithms : Certain algorithms can be easily implemented using array like searching and sorting, finding maximum and minimum elements.

How to declare an array in C++

Like any other variable in C++, we must declare an array before using it. Below is the syntax of declaring an 1D array.

data_type array_name[array_size];
Advantage of Arrays in C++
  • array_name : It is a valid C++ identifier.
  • array_size : It is the maximum number of elements which can be stored in array. It must be an integer constant greater than zero.
  • data_type : It represents the data type of each elements to be stored in array.
Examples of Array Declaration in C++
An Array of 10 Integers
     int age[10];
An Array of 50 Characters.
     char address[50];
Two Dimensional Array of Integers.
     int maze[20][20];

How to initialize an array in C++ programming

Arrays in C++ programming language is not initialized automatically at the time of declaration. By default, array elements contain garbage value. Like any other variable in C++, we can initialize arrays at the time of declaration.

Array Initialization by Specifying Array Size
int marks[7] = {5,2,9,1,1,4,7};

Array Initialization Without Specifying Array Size
int marks[] = {5,2,9,1,1,4,7};

In above declaration, compiler counts the number Of elements inside curly braces{} and determines the size of array score. Then, compiler will create an array of size 7 and initialize it with value provided between curly braces{} as discussed above.