Lets say I have an array like
int arr[10][10];
Now i want to initialize all elements of this array to 0. How can I do this without loops or
int arr[10][10] = {0}; // only in the case of 0
Besides the initialization syntax, you can always memset(arr, 0, sizeof(int)*10*10)
Defining a array globally will also initialize with 0.
#include<iostream>
using namespace std;
int ar[1000];
int main(){
return 0;
}
int myArray[2][2] = {};
You don't need to even write the zero explicitly.
You're in luck: with 0, it's possible.
memset(arr, 0, 10 * 10 * sizeof(int));
You cannot do this with another value than 0, because memset
works on bytes, not on int
s. But an int
that's all 0
bytes will always have the value 0
.
int arr[10][10] = { 0 };