[Solved] I am new to c++ why is casting working in first case but not in second cout after masking one of the characters in char* [closed]


Written as hex, 65280 is 0x0000FF00. So, on common systems with int being 4 bytes, you have set ch[0] to be 0. This is a null terminator, so when you try to print the string, you see an empty string.

Note: writing to *p causes undefined behaviour by writing past the end of the allocated area too; strdup("ab") allocates 3 bytes. On common systems this probably will have no ill effect as heap allocations are done in chunks of a certain size.

1

solved I am new to c++ why is casting working in first case but not in second cout after masking one of the characters in char* [closed]