C++ Arrays

The C++ arrays can be defined as a collection of items located stored at contiguous memory locations. Here, contiguous memory location means just after the first element of an array, the second element will be present and so on.  And these arrays are referenced by adding an index to a unique identifier. These arrays can store fixed-size sequential collection of elements of the same data type. We can have an array of Integers, Strings, Characters, etc. Once you have declared the array then we cannot change the size of the array.

C++ Arrays

When we have a small number of objects, but if we want to store a large number of instances then it becomes difficult to manage them with normal variables. The idea of an array is to represent many instances in one variable.

Advantages and Disadvantages of C++ Arrays

Advantages of C++ Arrays:



  • Code Optimization: Less code is required. And one variable can store a number of values.
  • Easy to traverse data: By using the arrays we can easily retrieve the data of an array.
  • Easy to sort data: We can easily sort the data by swapping technique.
  • Random Access: By using the array index we can randomly access any elements from the array.

Disadvantages of C++ Arrays:

Fixed Size: We cannot change the size once it is declared. And in case if you need more memory we cannot increase size. And also wastage of memory is done if you don’t use it.

Declaring an Array in C++

While you are declaring an array we can declare an array by size, type (or) by initializing (or) by both.  All the array elements can be accessed by using integer index. While the array index starts from 0 and goes till the size of array minus 1.

Syntax: type arrayName[arraySize];

C++ Arrays

Example 1: Array declaration by specifying the size

//Array declaration by specifying size 
int arr1[10]; 
int n = 10; 
int arr2[n];

Example 2: Array declaration by initializing elements

//Array declaration by initializing elements 
int arr[] = { 10, 20, 30, 40 } 
  
//Compiler creates an array of size 4. 
//above is same as  "int arr[4] = {10, 20, 30, 40}"

Example 3: Array declaration by specifying size and initializing elements

//Array declaration by specifying size and initializing elements 
int arr[6] = { 10, 20, 30, 40 } 
  
//Compiler creates an array of size 6, initializes first 
//4 elements as specified by user and rest two elements as 0. 
//above is same as  "int arr[] = {10, 20, 30, 40, 0, 0}"

Initializing an Array in C++

In C++ arrays can be initialized at the declaration time. And there are different ways to initialize an array.

  • By using one statement with square brackets.

Example: int anotherIntArray[3] = { 1, 2, 5, 7 };

  • We can omit the size of an array in the declaration.

Example: int anotherIntArray[] = { 1, 2, 5, 7 };

Example: for accessing elements in arrays

#include<iostream> 
using namespace std;
#include<iomanip> 
using std::setw;
int main() {
   int newArray[5];
   int n=0,p =0;
//Initializing elements of array seperately         
for(n=0; n<sizeof(newArray)/sizeof(newArray[0]);n++) {
 newArray[n] = n+50;
}
//print heading
 cout<<"Element"<<setw(10)<<"Value"<<endl;
//print element's value in loop                    
 for(p=0; p<sizeof(newArray)/sizeof(newArray[0]);p++) {
 cout<<setw(5)<<p<<setw(10)<<newArray[p]<<endl;
   }
   return 0;
}

Output:

Element    Value
0                 50
1                  51
2                 52
3                 53
4                 54