Skip to the content of the web site.

Arrays

In C++, unless you are writing an operating system or specialized high performance code, for the most part, you should be using std::array or std::vector. Now, unlike the Python list class, all entries of an array must be of the same type; however, this should not be much of an issue.

You can declare a local variable to be an array if you specify the number of entries in the array:

int main() {
    int array_name[10]{};   // Array declaration

    // Use this array...

    return 0;
}

Like Python, you can index into the array:

#include <iostream>

int main();

int main() {
    int my_array[10]{};   // Array declaration

    for ( std::size_t k{0}; k < 10; ++k ) {
        // Assign to the k'th entry of the array
        my_array[k] = k*k;
    }

    my_array[3] = 616;

    for ( std::size_t k{0}; k < 10; ++k ) {
        // Access the k'th entry of the array
        std::cout << my_array[k] << " ";
    }

    std::cout << std::endl;

    return 0;
}

Now, you may have noticed something peculiar: in the for-loop, we used a new type: std::size_t. If a local variable is used to index into an array, use this type, as its size will always be adjusted to allocate sufficient memory for the processor you are compiling for. For example, on a 32-bit system, all addresses are 32 bits, and thus the maximum an index can be is four bytes. On a 64-bit system, all addresses are 64 bits, and thus the maximum an index can be is eight bytes.

Like local variables, arrays go out of scope once execution leaves the block of statements in which it was declared. Thus, arrays can be used to temporarily store memory when necessary.

The major difference between arrays and the std::array class is that if you accidentally try to index beyond the array bounds (due to a programming error), then C++ will simply access the memory location, even though that memory may be used for something else, like another local variable!

You can view how assigning to other variables can occur due to assigning to array entries beyond the bounds by looking at this example in repl.it.

You can see how using the at(...) member function of the std::array class avoids this problem also at repl.it.

Capacity

Another weakness with arrays is that the array itself does not know its own capacity. Consequently, you must either:

  • hard-code the capacity of the array everywhere with literal integers, or
  • keep track of the array capacity in a separate local variable.

You don't have to know the array capacity at compilation time. The following code requests the user how many numbers would that user like to average, then asks the user for those numbers, and then calculates the average:

#include <iostream>

// Function declarations
int main();
double average( double array[], std::size_t capacity );

// Function definitions
int main() {
    std::cout << "Enter the number of values you want to average: ";

    std::size_t num_to_average;
    std::cin >> num_to_average;

    double values[num_to_average];

    for ( std::size_t k{0}; k < num_to_average; ++k ) {
        std::cout << "Enter number " << k << " to average: ";
        std::cin >> values[k];
    }

    std::cout << "The average is " << average( values, num_to_average )
              << std::endl;

    return 0;
}

double average( double array[], std::size_t capacity ) {
    double sum{0.0};

    for ( std::size_t k{0}; k < capacity; ++k ) {
       sum += array[k];
    }

    return sum/capacity;
}

You can try this example at repl.it.

You cannot do this with the std::array class, so if you wanted to do this without using an array, you need to use the std::vector class.