-4

enter image description here

I declared a pointer that pointed to an type of char array, and I changed one of the elements to '\0'. I think the function "strlen" can get the length for the first elements to the '\0' , but actually, the program crashed. Anybody knows the reason ?

littlenan
  • 1
  • 1

1 Answers1

0

Your character pointer points to a string literal, which is immutable data. For backwards compatibility reasons, the compiler allows a non-const char* to point to the literal, but as you noticed, attempting to modify the string crashed the program.

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157