Why do I get a segmentation fault when writing to a “char *s” initialized with a string literal, but not “char s[]”?

The following code receives seg fault on line 2: char *str = “string”; str[0] = ‘z’; // could be also written as *str=”z” printf(“%s\n”, str); While this works perfectly well: char str[] = “string”; str[0] = ‘z’; printf(“%s\n”, str); Tested with MSVC and GCC. 19 Answers 19