C++ Arrays4 min read

An array is a group or a fixed-size sequential collection of data having the same data-type stored in a contiguous memory location. It is a simple data structure format where the primitive type of data such as int, char, double, float, etc are stored and accessed randomly through their index number such as array[0], array[1], etc.

It can also store the collection of derived data types, such as pointers, structure, etc. Elements are arranged inside the array with index numbers starting from zero as the first elements.

Types of Array in C++:

  • One Dimensional Array
  • Multi-Dimensional Array

C++ Array Declaration

For declaration of an array in C++, the user needs to specify the type of element and number of elements in an array. Below is the declaration of the single-dimensional Array.

The data type must be a valid C++ data type, a unique name must be specified to each array and the arraySize must be of an integer constant. Let see an example for an integer array with 20 elements.


C++ Array Initialization

An array can be initialized by using a single statement or one by one. The Initialization is done within the curly braces {}. The example below is the initialization of single-dimensional Array.

In the second one of the above example, the array size is not declared so that you can initialize as much value as you want.

Users can also directly assign the value to the particular element in an array by specifying the index number such as: below shows how to assign the value 20 to the 3rd element of an array.

NOTE:
In the above example, number 2 means the 3rd element as the array index starts from 0.


Accessing Array Elements in C++

As told above, that each array elements are associated with specific number that is the index numbers.

An array element can be accessed by using the specific index number. It can be achieved by placing the particular index number within the bracket []. Such as:

We can also assign the value particular index number to other variable but their data type must be the same such as:

In the above, the value of 4th element from an array arr[] is assigned to the numb variable.


Example: C++ Program to show the declaration, initialization and accessing of an array

Output:


Empty Members in an array – C++

When we declare an array of size n, that means we can store the n number of elements of in that array. But what happens if we were to store less than n number of elements.

Consider an example below:

As you can see, the size of an array is 10 but we initialized it with only 6 elements. However, a 10 contiguous memory location is already allocated to this array. What will happen to the remaining un-initialized arrays?

Now in such cases, random values are assigned by the compiler to the remaining places. And often these random values are 0.


Index Out of bound : Accessing elements out of it bound.

Out of bound means, suppose you declare an array with 5 number of elements (index between 0 to 4) and you try to access the 7th element, which is not present in that array such as:

In the above program, the 7th element is not present but we are trying to print that element. In such cases, the program may run fine but you may get an unexpected output (undefined behavior) from a program.

There is no index out of bounds checking in C++.


Advantages of Array in C++

  • Less line of codes that is a single array with multiple elements, specifying code optimization.
  • Elements of an array can be accessed randomly with the index number.
  • The elements in an array can be easily traversed.
  • Array’s data are easy to manipulate.
  • Sorting becomes easy in an array with fewer lines of codes.

Disadvantages of Array in C++

  • The array is of fixed size once initialized. We cannot add new elements after initialization.
  • The number of elements you need to store in an array must be known in advance.
  • There may be a wastage of space if more memory is allocated than the required amount.

Arrays in C++

There is more to the arrays, click on the below to learn them separately in detail.

C ++ Multi-dimensional Array
C++ Passing Array to a Function
C++ Return Array from Functions

MORE

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …