Skip to main content

Arrays in C++

In this guide, we'll discuss arrays in C++. Arrays are a fundamental data structure that store multiple elements of the same type in a contiguous block of memory.

1. What is an Array?​

An array is a collection of elements of the same data type, stored in contiguous memory locations. Each element is accessed by its index, starting from 0.

2. Declaring an Array​

An array can be declared by specifying the data type of its elements, followed by the array name and the number of elements in square brackets.

Syntax:

type arrayName[arraySize];

Example:

int numbers[5]; // Declares an array of 5 integers

3. Initializing an Array​

You can initialize an array at the time of declaration by providing values in curly braces.

Example:

int numbers[5] = {1, 2, 3, 4, 5}; // Initializes an array with 5 elements

If you don't specify all the elements, the remaining ones are set to 0 by default.

Example:

int numbers[5] = {1, 2}; // Remaining elements are set to 0

4. Accessing Array Elements​

You can access individual elements of an array using the array name followed by the index in square brackets.

Example:

#include <iostream>
using namespace std;

int main() {
int numbers[5] = {1, 2, 3, 4, 5};
cout << "The first element is: " << numbers[0] << endl;
return 0;
}

Output:

The first element is: 1

5. Array in Loops​

You can use loops to iterate over an array and access each element.

Example:

#include <iostream>
using namespace std;

int main() {
int numbers[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
cout << "Element " << i << " is: " << numbers[i] << endl;
}
return 0;
}

Output:

Element 0 is: 1
Element 1 is: 2
Element 2 is: 3
Element 3 is: 4
Element 4 is: 5

6. Multi-dimensional Arrays​

C++ allows you to create arrays with more than one dimension. A two-dimensional array, for instance, can be used to represent a matrix.

Example:

#include <iostream>
using namespace std;

int main() {
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};

for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
cout << matrix[i][j] << " ";
}
cout << endl;
}
return 0;
}

Output:

1 2 3
4 5 6

7. Array of Strings​

You can create an array of strings by declaring a two-dimensional array of characters.

Example:

#include <iostream>
using namespace std;

int main() {
const char* fruits[3] = {"Apple", "Banana", "Cherry"};

for (int i = 0; i < 3; i++) {
cout << fruits[i] << endl;
}

return 0;
}

Output:

Apple
Banana
Cherry

8. Array Limitations​

While arrays in C++ are useful, they have some limitations:

  • The size of an array is fixed at the time of declaration and cannot be changed during runtime.
  • Arrays do not provide bounds checking, meaning accessing an index out of range may result in undefined behaviour.
  • No built-in support for dynamic resizing or higher-level operations such as sorting and searching.

For more advanced functionality, C++ provides other data structures like vectors (from the STL) that address many of these limitations.

Final Thoughts​

Arrays are an essential part of C++ programming, especially when dealing with fixed-size collections of elements. However, for dynamic and resizable collections, it's better to use other data structures like vectors.