C

Asked • 06/05/19

Why do I get a segmentation fault when writing to a string initialized with "char *s" 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.

1 Expert Answer

By:

Al P. answered • 06/05/19

Tutor
0 (0)

Online Mathematics tutor

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.