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

Leave a Reply

Your email address will not be published. Required fields are marked *