Why is memset() function being used?

Can’t we just do pre[100000]={0} instead?

hey@mehulbhandari358
yes you can use ={0} instead but there are some loopholes about it.

you can initialize each element

int myArray[10] = { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };

Elements with missing values will be initialized to 0:

int myArray[10] = { 1, 2 }; // initialize to 1,2,0,0,0...

So this will initialize all elements to 0:

int myArray[10] = { 0 }; // all elements 0

In C++, an empty initialization list will also initialize every element to 0.:

int myArray[10] = {}; // all elements 0 in C++

Remember that objects with static storage duration will initialize to 0 if no initializer is specified:

static int myArray[10]; // all elements 0

please mark your doubt as resolved if this does

@mehulbhandari358 we have multiple test cases here so if you want to use pre[100000]={0} then you have to initialize pre array inside while(test–) loop since for every test case pre array should start with all values 0. and if you are initializing it outside while loop then you have to use memset().

with memset there is no need to initialize it in the while loop
why ??

here you want all value to be initialize with 0 as pre[10000]={0} and we want all values of array 0 inside while loop for every test case, so either you initialize it inside while loop as pre[100000]={0} or define your array outside while loop and only use memset function inside while loop to put all values 0 in either of the cases each test case will start with all values 0 of pre array

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.