memset-intializing arrays
Jul 29, 2014 at 5:08pm UTC
1 2 3 4 5 6 7 8 9 10 11 12
#include<iostream>
#include <stdio.h>
#include <cstring>
using namespace std;
int main() {
int test[3];
memset(test,1,sizeof (test));
for (int i=0;i<3;i++){
cout<<test[i]<<endl;
}
return 0;
}
o/p:
16843009
16843009
16843009
why is the output not 1 1 1?I needed to store 1 in all the indices of the array test.But,I end up getting some weird output.Please help.
Jul 29, 2014 at 5:11pm UTC
memset is character based. If an int is 32 bits in your environment, memdet will store a 1 in each byte of the int. memset knows nothing about initializing ints,.
Topic archived. No new replies allowed.