Skip to main content
2 of 2
add more about strings
curiouskiwi
  • 18.7k
  • 2
  • 18
  • 43

string s = "hello"; declares a string literal. By definition, it is immutable. If you try to change a char in that string, you will segfault.

Have a look at my post about string literals for more info.

Also, you'll learn more about this in Week 4, but your line string s_capital = s; does not make a copy of the string called s. It simply creates a new variable that "points" to the same part of memory. That is why you cannot change s_capital either.

curiouskiwi
  • 18.7k
  • 2
  • 18
  • 43