I have an array of int and i have to initialize this array with the value -1. For now i use this loop:
int i;
int myArray[10];
for(i = 0; i < 10; i++)
myArray[i] = -1;
There are faster ways?
The quickest way I know for the value -1
(or 0
) is memset:
int v[10];
memset(v, -1, 10 * sizeof(int));
Anyway you can optimize the loop in this way:
int i;
for(i = 10; i--;)
v[i] = -1;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With