c++ initialize array with all zeros
To initialize an array with all zeros in C++, you can use the following code:
int array[10] = {0};
This code defines an array called array
with a size of 10 elements. The = {0}
part initializes all elements of the array to 0.
Here is a step-by-step explanation of the code:
Array declaration:
int array[10]
declares an array calledarray
of typeint
with a size of 10 elements. The square brackets[]
indicate that it's an array, and the number inside specifies the number of elements in the array.Initialization: The
= {0}
part initializes all elements of the array to 0. The curly braces{}
are used to enclose the values to be assigned to the array elements. In this case,{0}
means that the first element is initialized to 0, and the remaining elements are automatically initialized to 0 as well.
By using this code, you can create an array in C++ and initialize all of its elements to 0. This is useful when you want to start with a clean slate and set all values to a known default state.