I want to initialize the last 4 bytes of a char array with 0 (set all 32 bits to zero). But the assignment is changing only one byte in the array. How can I change this byte and the next three in a single command, instead of looping through all 4 bytes? Is this possible?
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
char buf[8 + 4]; // 8 bytes of garbage + 4 = 32 safety bits
buf[8] = (uint32_t)0; // turns all safety bits into zero???
cout << hex << setfill(' ');
for (int i=0; i<8 + 4; i++) {
cout << setw(3) << (int)buf[i];
}
cout << dec << endl;
return 0;
}
That's displaying:
0 9 40 0 0 0 0 0 0 8 40 0
^ ^ ^ ^
ok garbage undesired