Skip to main content
add more about strings
Source Link
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.

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.

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.

Source Link
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.