I am a bit curious why my code prints a number instead of bool value.
class fb { public: bool p; void func() { memset(&(p), 4, 1); } }; int main() { fb f; f.func(); std::cout << f.p << std::endl; return 0; } My code prints number "4" and I am not really sure why this happens. Can you please explain me what is wrong with my code? I was expecting that result will be "true".
Thank you
bool. You canmemcpythem, doesn't mean you canmemsetthem.memset's second parameter is the value, the third is the number of bytes to set to that value.static_cast<bool>(4)==true, andsizeof(bool)might be 1, I'm not sure if that is an actual mistake. This is just bad code by any standard.