How to Reset int Array to Zero in C++? Last Updated : 04 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, arrays store data of the same type in a contiguous memory location. To reset an int array means setting all elements to zero. In this article, we will see how to reset an integer array elements to zero in C++. For Example, Input: int arr[] = { 10, 20, 30, 40, 50 };Output:Array after resetting: 0 0 0 0 0 Reset C++ int Array to ZeroIn C++, there is no direct function to reset an array but it can be done by using the memset() function from the cstring library to set each element to zero. Syntax of memset()memset(arr, val, n);Here, arr is a pointer to the array.val is the default value that is used to replace array elements (here 0).n is number of bytes to copy.C++ Program to Reset an int Array to ZeroThe below program demonstrates how we can reset an int array to zero in C++ using the memset function. C++ // C++ Program to illustrate how to reset an int array to // zero #include <cstring> #include <iostream> using namespace std; int main() { // Initialize an int array int arr[] = { 10, 20, 30, 40, 50 }; int n = sizeof(arr) / sizeof(arr[0]); // print the original array cout << "Original array: "; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; // Reset the array by setting each element to 0 using // memset memset(arr, 0, n * sizeof(arr[0])); cout << "Array after resetting: "; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; return 0; } OutputOriginal array: 10 20 30 40 50 Array after resetting: 0 0 0 0 0 Time Complexity: O(N), where N is the size of the array.Auxiliary Space: O(1) Note: We can also use std::fill() function to reset an int array to zero in C++. Comment More infoAdvertise with us Next Article How to Replace a Value in an Array in C++? D denzirop9v Follow Improve Article Tags : C++ Programs C++ cpp-array CPP Examples Practice Tags : CPP Similar Reads How to Replace a Value in an Array in C++? In C++, an array is a data structure that is used to store multiple values of similar data types in a contiguous memory location. In this article, we will learn how to replace a value in an array in C++. Example: Input: arr: {10 20 30 30 20 10 10 20} Element_to_replace= 20 replacement=99 Output: Arr 2 min read How to Print an Array in C++? In C++, an array is a fixed-size linear data structure that stores a collection of elements of the same type in contiguous memory locations. In this article, we will learn how to print an array in C++. For Example, Input: array = {10, 20, 30, 40, 50}Output: Array Elements: 10 20 30 40 50Printing Arr 2 min read How to Initialize a Vector with Zero in C++? Initializing a vector with value zero means assigning the initial value 0 to all elements of vector. In this article, we will learn the different methods to initialize the vector with zero in C++.The simplest method to initialize a vector with zeros is by using vector constructor. Let's take a look 2 min read How to Reverse an Array using STL in C++? Reversing an array means rearranging its elements so that the first element becomes the last, the second element becomes the second last, and so on. In this article, we will learn how to reverse an array using STL in C++.The most efficient way to reverse an array using STL is by using reverse() func 2 min read How to Initialize an Array in C++? In C++, an array is a collection of similar datatypes stored in contiguous memory locations in which each element can be accessed using their indices. In this article, we will learn how to initialize an array in C++. Initializing an Array in C++To initialize an array in C++, we can use the assignmen 2 min read How to Loop Over an Array in C++? In C++, an array is a data structure that stores elements of similar type in contiguous memory locations. We can access the elements of an array using array indexing. In this article, we will learn how to loop over an array in C++. Example: Input: int arr[] = [1, 2, 3, 4, 5] Output: Array Elements: 2 min read Like