In this C++ tutorial, you will learn how to initialize arrays with values, initialize all elements to zero, let the compiler determine the array size, initialize arrays of different data types, and work with arrays of structures and class members.

How to initialize an array in C++

A built-in C++ array can be initialized when it is declared by placing its initial values inside braces {}. The values are written in array-index order, starting with the element at index 0.

</>
Copy
data_type array_name[size] = {value1, value2, value3, ...};

For example, the following declaration creates an integer array of five elements and initializes every element.

</>
Copy
int numbers[5] = {10, 20, 30, 40, 50};

For a built-in array, the brace-enclosed list initializes the array as part of its declaration. You cannot later assign another brace-enclosed list to the entire built-in array with an expression such as numbers = {1, 2, 3};. After declaration, individual elements can be changed by index, or an algorithm can be used to replace multiple elements.

C++ array initialization during declaration

Initialize a C++ array without specifying its size

We can assign the list of elements to the array variable during declaration. If we do not specify the size of array, the number of elements we are initializing the array with, becomes the size of array.

In the following example, we have not specified any size in the square brackets.

</>
Copy
int a[] = {7, 3, 8, 7, 2};

But, as we have assigned a list of five elements to the array, the size of the array becomes 5.

This form is useful when the initializer itself already determines how many elements are required. The compiler counts the values and creates an array large enough to hold all of them.

Initialize a C++ array with a specified size and fewer values

We can also specify the size of array, and assign a list of elements to the array. The number of elements in the list could be less than the size specified. Then, the compiler creates array with specified size, and assigns the elements one by one from the starting of array.

</>
Copy
int a[10] = {7, 3, 8, 7, 2, 9, 5};

We have specified the array size to be 10.  But we have given only seven elements in the initializing list. So, the first seven elements of the array get initialized with the given list of elements, and the rest of array elements are initialized to zero.

More precisely, the remaining elements are value-initialized. For arithmetic types such as int, float, and double, this produces zero. For bool, it produces false. For class types, their appropriate default or value initialization rules apply.

Initialize C++ array elements later using indexes

We can also declare an array with a specific size and initialize later, one element at a time, using index.

</>
Copy
int a[10];
a[0] = 7;
a[1] = 3;
a[2] = 8;
a[3] = 2;
a[4] = 9;

In this case, only the elements explicitly assigned above receive those values. A local built-in array declared as int a[10]; without an initializer does not automatically initialize its elements to zero. Reading an element before assigning a valid value to it should be avoided.

Initialize all elements of a C++ array to zero

To initialize every element of a built-in numeric array to zero, use an empty brace initializer or provide 0 as the first initializer. The remaining elements are value-initialized.

</>
Copy
#include <iostream>

int main() {
    int a[5]{};
    int b[5] = {0};

    for (int value : a) {
        std::cout << value << " ";
    }

    std::cout << '\n';

    for (int value : b) {
        std::cout << value << " ";
    }
}

Output

0 0 0 0 0
0 0 0 0 0

The form int a[5]{} is concise modern C++ syntax for value-initializing every element.

Initialize every C++ array element to the same non-zero value

A declaration such as int a[5] = {7}; does not fill the whole array with 7. It initializes only the first element to 7 and value-initializes the remaining elements to zero.

</>
Copy
#include <algorithm>
#include <iostream>
#include <iterator>

int main() {
    int a[5];

    std::fill(std::begin(a), std::end(a), 7);

    for (int value : a) {
        std::cout << value << " ";
    }
}

Output

7 7 7 7 7

std::fill() is an appropriate way to assign the same value to every element of an existing array.

C++ initialize array of integers

In this example, we initialize two arrays of integers: one array with size not specified during declaration and the second with size specified and fewer elements initialized.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

int main() {
   //initialize array with no size specified
   int arr1[] = {7, 3, 8, 7, 2};
   //initialize array with size specified, but with fewer elements
   int arr2[10] = {52, 78, 18, 92, 96, 69, 84};

   //print arrays
   for (int i=0; i < sizeof(arr1)/sizeof(*arr1); i++) {
      cout << arr1[i] << "  ";
   }
   cout << endl;
   for (int i=0; i < sizeof(arr2)/sizeof(*arr2); i++) {
      cout << arr2[i] << "  ";
   }
}

Output

7  3  8  7  2
52  78  18  92  96  69  84  0  0  0

arr1 gets a size of five from its initializer. arr2 has an explicit size of ten; its seven supplied integers occupy indexes 0 through 6, and the final three elements are initialized to zero.

C++ initialize array of strings

In this example, we initialize two arrays of strings: one array with size not specified during declaration and the second with size specified and fewer elements initialized.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

int main() {
   //initialize array with no size specified
   string arr1[] = {"hello" , "hi", "good" , "morning"};
   //initialize array with size specified, but with fewer elements
   string arr2[10] = {"hello" , "hi", "good" , "morning", "thank", "you", "user"};

   //print arrays
   for (int i=0; i < sizeof(arr1)/sizeof(*arr1); i++) {
      cout << arr1[i] << "  ";
   }
   cout << endl;
   for (int i=0; i < sizeof(arr2)/sizeof(*arr2); i++) {
      cout << arr2[i] << "  ";
   }
}

Output

hello  hi  good  morning
hello  hi  good  morning  thank  you  user

The unused elements of arr2 are value-initialized std::string objects, so they contain empty strings. Printing them produces no visible text after user.

C++ initialize array of chars

In this example, we initialize two arrays of chars: one array with size not specified during declaration and the second with size specified and fewer elements initialized.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

int main() {
   //initialize array with no size specified
   char arr1[] = {'a', 'e', 'i', 'o', 'u'};
   //initialize array with size specified, but with fewer elements
   char arr2[10] = {'t', 'u', 't', 'o', 'r', 'i', 'a', 'l'};

   //print arrays
   for (int i=0; i < sizeof(arr1)/sizeof(*arr1); i++) {
      cout << arr1[i] << "  ";
   }
   cout << endl;
   for (int i=0; i < sizeof(arr2)/sizeof(*arr2); i++) {
      cout << arr2[i] << "  ";
   }
}

Output

a  e  i  o  u
t  u  t  o  r  i  a  l

The final two elements of arr2 are initialized to the null character '\0'. They do not produce visible characters when sent to the output stream.

Initialize a C++ char array from a string literal

A character array can also be initialized from a string literal. In this form, the array includes an additional null character at the end so that it can represent a C-style string.

</>
Copy
#include <iostream>
#include <iterator>

int main() {
    char word[] = "hello";

    std::cout << word << '\n';
    std::cout << std::size(word);
}

Output

hello
6

Although the word has five visible letters, the array contains six char elements because the terminating '\0' is also stored.

C++ initialize array of floating point numbers

In this example, we initialize two arrays of floating point numbers: one array with size not specified during declaration and the second with size specified and fewer elements initialized.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

int main() {
   //initialize array with no size specified
   float arr1[] = {7.98652, 5.7443, 8.88888, 4.1255557, 2.22222};
   //initialize array with size specified, but with fewer elements
   float arr2[10] = {5.882, 5.578, 1.448, 92.232, .74965, .6669, 888.884};

   //print arrays
   for (int i=0; i < sizeof(arr1)/sizeof(*arr1); i++) {
      cout << arr1[i] << "  ";
   }
   cout << endl;
   for (int i=0; i < sizeof(arr2)/sizeof(*arr2); i++) {
      cout << arr2[i] << "  ";
   }
}

Output

7.98652  5.7443  8.88888  4.12556  2.22222
5.882  5.578  1.448  92.232  0.74965  0.6669  888.884  0  0  0

The final three elements of arr2 are initialized to floating-point zero because fewer initializers were supplied than the declared array size.

C++ initialize array of long integers

In this example, we initialize two arrays of long numbers: one array with size not specified during declaration and the second with size specified and fewer elements initialized.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

int main() {
   //initialize array with no size specified
   long arr1[] = {798652, 57443, 888888, 41255557, 222222};
   //initialize array with size specified, but with fewer elements
   long arr2[10] = {5882, 5578, 1448, 92232, 74965, 6669, 888884};

   //print arrays
   for (int i=0; i < sizeof(arr1)/sizeof(*arr1); i++) {
      cout << arr1[i] << "  ";
   }
   cout << endl;
   for (int i=0; i < sizeof(arr2)/sizeof(*arr2); i++) {
      cout << arr2[i] << "  ";
   }
}

Output

798652  57443  888888  41255557  222222
5882  5578  1448  92232  74965  6669  888884  0  0  0

C++ initialize array of booleans

In this example, we initialize two arrays of boolean values: one array with size not specified during declaration and the second with size specified and fewer elements initialized.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

int main() {
   //initialize array with no size specified
   bool arr1[] = {true, true, false, true, false};
   //initialize array with size specified, but with fewer elements
   bool arr2[10] = {true, false, false, false, false, true, true};

   //print arrays
   for (int i=0; i < sizeof(arr1)/sizeof(*arr1); i++) {
      cout << arr1[i] << "  ";
   }
   cout << endl;
   for (int i=0; i < sizeof(arr2)/sizeof(*arr2); i++) {
      cout << arr2[i] << "  ";
   }
}

Output

1  1  0  1  0
1  0  0  0  0  1  1  0  0  0

By default, std::cout displays true as 1 and false as 0. Because only seven initializers are provided for arr2, its last three elements are value-initialized to false.

Initialize an array of structs in C++

An array can contain structures as its element type. Each item in the outer initializer list initializes one structure object, while the inner braces provide values for that object’s members.

</>
Copy
#include <iostream>
#include <string>

struct Product {
    std::string name;
    double price;
};

int main() {
    Product products[] = {
        {"Mouse", 500.0},
        {"Keyboard", 1200.0},
        {"Monitor", 15000.0}
    };

    for (const Product& product : products) {
        std::cout << product.name << ": " << product.price << '\n';
    }
}

Output

Mouse: 500
Keyboard: 1200
Monitor: 15000

The compiler determines the array size as three because three Product objects are present in the initializer.

Initialize a two-dimensional array in C++

A multidimensional array can be initialized with nested braces. Each inner initializer represents one row of the array.

</>
Copy
#include <iostream>

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

    for (const auto& row : matrix) {
        for (int value : row) {
            std::cout << value << " ";
        }
        std::cout << '\n';
    }
}

Output

1 2 3
4 5 6

When the first dimension is omitted, the compiler may infer it from the initializer, provided the later dimensions are still specified. For example, int matrix[][3] = {{1, 2, 3}, {4, 5, 6}}; creates two rows.

Initialize an array member in a C++ constructor

If a class contains a built-in array as a data member, one simple approach in modern C++ is to give that member an in-class initializer. The constructor can then use the initialized array without assigning each element separately.

</>
Copy
#include <iostream>

class Scores {
public:
    Scores() = default;

    void print() const {
        for (int score : values) {
            std::cout << score << " ";
        }
    }

private:
    int values[4] = {10, 20, 30, 40};
};

int main() {
    Scores scores;
    scores.print();
}

Output

10 20 30 40

Built-in C++ array initialization compared with std::array

For fixed-size collections, C++ also provides std::array. It uses similar brace initialization but behaves more like a standard container and provides member functions such as size(), begin(), and end().

</>
Copy
#include <array>
#include <iostream>

int main() {
    std::array<int, 4> values = {5, 10, 15, 20};

    for (int value : values) {
        std::cout << value << " ";
    }
}

Output

5 10 15 20

C++ array initialization rules to remember

  • A built-in array may be initialized with a brace-enclosed list when it is declared.
  • If the array size is omitted, the compiler can infer it from the number of initializers.
  • If an explicit size is larger than the initializer list, the remaining elements are value-initialized.
  • If more initializers are supplied than the declared array can hold, the program is ill-formed and the compiler must diagnose the problem.
  • int a[5]{}; value-initializes all five integers to zero.
  • int a[5] = {7}; initializes only the first element to 7; it does not fill all five elements with 7.
  • A local built-in array declared without an initializer, such as int a[5];, does not automatically contain zeros.
  • A built-in array cannot be assigned another complete initializer list after declaration.

Common C++ array initialization mistakes

The following cases are easy to confuse when first working with arrays.

</>
Copy
// First element is 5; remaining elements become 0
int a[4] = {5};

// All elements become 0
int b[4]{};

// Size is inferred as 4
int c[] = {5, 10, 15, 20};

// Declared but not initialized
int d[4];

Another common error is trying to provide too many values:

</>
Copy
// Invalid: five initializers for an array of size four
int values[4] = {10, 20, 30, 40, 50};

The number of explicitly supplied initializers cannot exceed the number of elements in the declared array.

Editorial QA checklist for C++ array initialization

  • Verify that built-in array initialization is described as occurring at declaration rather than as whole-array assignment after declaration.
  • Verify that omitted array sizes are described as being inferred from the initializer count.
  • Verify that partially initialized arithmetic arrays correctly show zero for the remaining elements.
  • Verify that {value} is not described as filling every array element with that value.
  • Verify that zero initialization examples distinguish int a[5]{}; from an uninitialized local declaration such as int a[5];.
  • Verify that character arrays initialized from string literals account for the terminating null character.
  • Verify that array-of-struct and multidimensional-array examples use initializer nesting that matches their element structure.
  • Verify that any method used to fill an existing array with one repeated value assigns every element explicitly, such as with std::fill().

C++ array initialization summary

Use a brace-enclosed initializer when declaring a built-in C++ array. You may specify the array size explicitly or allow the compiler to infer it from the values. When fewer values than the declared size are supplied, the remaining elements are value-initialized. Use empty braces to initialize numeric elements to zero, and use an algorithm such as std::fill() when an existing array must be filled with the same non-zero value.

In this C++ Tutorial, we learned how to initialize an array in C++ in different ways, and for different datatypes, with example C++ programs.