1. Help with logic

Im trying to replace this for() statement with memset() in a program,
which seem to me the exact same thing,
but i dont get the same result with memset() in the program.

Code:
```int pass = 4;
int array[pass-1];

for(; j<pass; j++) array[j]=0;

memset(array,'\0',pass-1);```

Code:
`int array[pass] = { 0 };`
if you're just doing it once?

3. Well, you could try this way,

Code:
```#include <iostream>
#include <vector>
using namespace std;

vector<int> test(10);

int main(int argc, char *argv[])
{
fill(test.begin(), test.begin()+10, 0);
for (int i = 0; i < test.size(); ++i)
{
cout << test[i] << " ";
}
return 0;
}```

Also, there's an error here:
Code:
```int pass = 4;
int array[pass-1]; // only three indices declared, array[0,2].
for(; j<pass; j++) array[j]=0; // out of bounds check on j = 3 ***```
You probably meant;

Code:
```int pass = 4;
int array[pass];```

Well i thought that in my example both meant to initialize every element of the array to 0.

@Rags_to_riches example is simplier but i wanna initialize it dynamicaly so i guess i
would have to use "new" + memset and that would be more code than just to use the for() loop.
Code:
```cin  >>  pass;
int * array = new int[pass];
memset(array,'\0',pass);```
@Since, your first example is too much code for just to initialize to zero.

And then you are right its out of bounds check on j = 3. Good catch. Thanks.
Though its interesting that if i do it the right way like this, i get the same wrong result as with memset().

Code:
`for(; j<pass-1; j++) array[j]=0;`
So to be on the safe side i guess i have to go with dynamic allocation.
As i cant use c++ string, its not an int.

Now i realize thats why you talked about vectors.
Cool! Problem solved with vector.

5. Originally Posted by Ducky
@Since, your first example is too much code for just to initialize to zero.
That is true. std::fill is not needed, because the vector is constructed to value initialise its elements, and value initialisation for elements of type int means zero initialisation. As such, the example could be written as:
Code:
```#include <iostream>
#include <vector>
using namespace std;

int main()
{
vector<int> test(10);
for (vector<int>::size_type i = 0; i < test.size(); ++i)
{
cout << test[i] << ' ';
}
return 0;
}```

6. Awesome, thank you Laserlight!