<p>The following code receives seg fault on line 2:</p>

<pre><code>char *str = "string";
str[0] = 'z';  // could be also written as *str = 'z'
printf("%s\n", str);
</code></pre>

<p>While this works perfectly well:</p>

<pre><code>char str[] = "string";
str[0] = 'z';
printf("%s\n", str);
</code></pre>

<p>Tested with MSVC and GCC.</p>

Because the type of
```
"whatever"
```
in the context of the 1st example is
```
const char *
```
(even if you assign it to a non-const char*), which means you shouldn't try and write to it.
The compiler has enforced this by putting the string in a read-only part of memory, hence writing to it generates a segfault.