Declare an Array in C++

This tutorial will discuss multiple ways to declare an array in C++.

Table Of Contents

Declate an non-initialized Array in C++

Suppose you want to declare an array of 5 integers. For that, we can write the data type, followed by the variable name. Inside square brackets, we specify the number of elements we want in the array. Like this,

int arr[5];

By doing this, it will declare an array with 5 elements, each an integer. An important point to note here is that all the integers in this array will not be initialized; they will contain garbage values.

We can determine the size of the array using the sizeof operator.

size_t len = sizeof(arr) / sizeof(arr[0]);

Since we have not initialized the array while declaring it, it contains garbage values. We can access individual elements in the array and initialize them using square brackets.

// Access and modify array elements
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;

To access the first element of the array, we use index position zero. To access the second element, we use index position one. Similarly, to access the nth element in the array, we use index position (n-1) with the square brackets.

After initializing, we can iterate over the array and print each element.

// Access and print array elements
for (int i = 0; i < len; ++i)
{
    std::cout << "arr["
                << i
                << "] = "
                << arr[i]
                << std::endl;
}

Let’s see the complete example,

#include <iostream>

int main()
{
    // Declare an array of integers
    // with a size of 5
    int arr[5];

    // Get the size of array
    size_t len = sizeof(arr) / sizeof(arr[0]);

    // Access and modify array elements
    arr[0] = 10;
    arr[1] = 20;
    arr[2] = 30;
    arr[3] = 40;
    arr[4] = 50;

    // Access and print array elements
    for (int i = 0; i < len; ++i)
    {
        std::cout << "arr["
                  << i
                  << "] = "
                  << arr[i]
                  << std::endl;
    }

    return 0;
}

Output

arr[0] = 10
arr[1] = 20
arr[2] = 30
arr[3] = 40
arr[4] = 50

Declate an Array and Initialze with hard coded values

In the previous example, we declared an array and left its values uninitialized. If you want to initialize an array with hardcoded values while declaring it, you can provide the values inside curly braces. In this case, we don’t need to specify the size of the array in the square brackets during its declaration, as the compiler will deduce it automatically from the provided initial values. Like this,

int arr[] = {78, 89, 10, 61, 55, 44};

It will create an array of size integers and initialize it with six hard coded integer values.

When an array is initialized with hardcoded values, we can get the size or the number of elements using the sizeof operator.

size_t len = sizeof(arr) / sizeof(arr[0]);

The array is already initialized, so we can iterate over its indices, access each element using square brackets, and print it to the console.

for (int i = 0; i < len; ++i)
{
    std::cout << "arr["
                << i
                << "] = "
                << arr[i]
                << std::endl;
}

Let’s see the complete example,

#include <iostream>

int main()
{
    // Declare an array of integers
    // with hard coded values
    int arr[] = {78, 89, 10, 61, 55, 44};

    // Get the size of array
    size_t len = sizeof(arr) / sizeof(arr[0]);

    // Access and print array elements
    for (int i = 0; i < len; ++i)
    {
        std::cout << "arr["
                  << i
                  << "] = "
                  << arr[i]
                  << std::endl;
    }

    return 0;
}

Output

arr[0] = 78
arr[1] = 89
arr[2] = 10
arr[3] = 61
arr[4] = 55
arr[5] = 44

Declate an Array and Initialze with same values

Suppose you want to declare an array and initialize all its elements with the value zero. In that case, you can provide a single value inside curly braces.

int arr[5] = {0};

This will create an array of 5 elements and initialize all the integers with the value zero.

To determine the number of elements in the array, we use the sizeof operator.

size_t len = sizeof(arr) / sizeof(arr[0]);

Now, our array is declared and initialized with zero values, so we can iterate over all the elements using a for loop and print each one.

for (int i = 0; i < len; ++i)
{
    std::cout << "arr["
                << i
                << "] = "
                << arr[i]
                << std::endl;
}

Let’s see the complete example,

#include <iostream>

int main()
{
    // Declare an array of integers
    // with all zeros
    int arr[5] = {0};

    // Get the size of array
    size_t len = sizeof(arr) / sizeof(arr[0]);

    // Access and print array elements
    for (int i = 0; i < len; ++i)
    {
        std::cout << "arr["
                  << i
                  << "] = "
                  << arr[i]
                  << std::endl;
    }

    return 0;
}

Output

arr[0] = 0
arr[1] = 0
arr[2] = 0
arr[3] = 0
arr[4] = 0

Summary

We learned about three different ways to declare and initialize an Array in C++.

Summary

Today, we learned about multiple ways to declare an array in C++.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top