Why can't we put a 0 character in a char pointer?
It crashes on both lines.
Code:int main() { char * buf = "hello world"; buf[5] = '\0'; buf+5 = '\0'; return 0; }
Why can't we put a 0 character in a char pointer?
It crashes on both lines.
Code:int main() { char * buf = "hello world"; buf[5] = '\0'; buf+5 = '\0'; return 0; }
Using Windows 10 with Code Blocks and MingW.
It's because buf is pointing to a string constant, and the OS steps in and kills the program when it attempts to modify read-only memory.
Put it in a char array for success.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Ah yeah, of course I knew that and forgot about it.
I just thought when you pass a char array in a function you could modify it but its the same.
Thanks Salem!
Using Windows 10 with Code Blocks and MingW.