cpp array init value

Here is an example of how to initialize an array in C++:

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

Explanation:

  1. int myArray[5] declares an array named myArray of type int with a size of 5 elements. The square brackets [5] indicate the number of elements in the array.

  2. = {1, 2, 3, 4, 5} initializes the array with the values 1, 2, 3, 4, and 5. The values are enclosed in curly braces {} and separated by commas.

In this example, the array myArray is declared with a size of 5 elements and is initialized with the values 1, 2, 3, 4, and 5. Each element of the array can be accessed using its index, starting from 0. For example, myArray[0] will give you the value 1, myArray[1] will give you the value 2, and so on.

It's important to note that if you don't provide enough values to initialize the array, the remaining elements will be initialized to their default values. For example, if you declare int myArray[5] = {1, 2, 3};, the first three elements will have the values 1, 2, and 3, and the remaining two elements will have the default value of 0.

Let me know if you need further assistance!