4 minute read

Introduction

Arrays are a fundamental data structure in C++ that allow us to store and manipulate a collection of elements of the same data type. An array is a contiguous block of memory that stores a fixed number of elements, each of which can be accessed by its index. In this tutorial, we will learn how to use arrays in C++.

Declaring Arrays

To declare an array in C++, we use the [] brackets after the data type and give it a name. The number of elements in the array is specified within the brackets. For example, to declare an array of integers that can hold 10 elements, we use the following syntax:

int myArray[10];

We can also initialize the elements of an array at the time of declaration by providing a list of values separated by commas:

int myArray[5] = {1, 2, 3, 4, 5};

Accessing Array Elements

We can access the elements of an array using the array name followed by the index of the element within square brackets. The index of an element in an array starts from 0 and goes up to the size of the array minus 1. For example, to access the first element of the array myArray, we use the following syntax:

int firstElement = myArray[0];

Array Size

To get the size of an array in C++, we use the sizeof operator. The sizeof operator returns the size of the array in bytes. To get the number of elements in an array, we divide the size of the array by the size of an individual element. For example, to get the number of elements in the array myArray, we use the following syntax:

int size = sizeof(myArray) / sizeof(myArray[0]);

Iterating over an Array

We can iterate over an array using a for loop to access and manipulate each element of the array.

for(int i = 0; i < size; i++) {
    std::cout << myArray[i] << std::endl;
}

Full example

#include <iostream>
using namespace std;

int main() {
    // Initialize an array with size 5
    int myArray[5] = {1, 2, 3, 4, 5};

    // Add an element to the array
    myArray[5] = 6;

    // Access an element of the array
    cout << "The first element of the array is: " << myArray[0] << endl;

    // Iterate through the array
    for (int i = 0; i < 6; i++) {
        cout << "Element " << i << ": " << myArray[i] << endl;
    }

    // Remove an element of the array
    for (int i = 0; i < 5; i++) {
        myArray[i] = myArray[i + 1];
    }

    // Modify an element of the array
    myArray[4] = 10;

    // Delete the array
    delete[] myArray;

    return 0;
}

Note: As per the above example, since array size is fixed, if you want to add a element you need to reallocate memory and copy the existing elements. Also, the delete[] is not necessary as the array goes out of scope at the end of the function, but this will help you understand how to delete an array in C++.

Conclusion

In this tutorial, we have learned about arrays in C++ and how to use them. We have seen how to declare and initialize arrays, how to access and manipulate their elements, and how to iterate over them. Arrays are a fundamental data structure in C++ and understanding how to use them is an essential part of mastering the language. With arrays, we can store and manipulate a collection of elements of the same data type, making them an important tool for solving many programming problems.

References

Read more about C++ Arrays on https://cplusplus.com/doc/tutorial/arrays/