为什么访问冲突写入内存位置

Why Access violation writing at memory location

我有以下代码。

1 #include <stdio.h>
2 #include <string.h>
3 
4 void encryptString2(char *encryptedString)
5 {
6   
7   while (*encryptedString)
8   {   
9       *encryptedString = *encryptedString ^ 31;
10      printf("Encrypted Character : %c\n", *encryptedString);
11      encryptedString++;  
12  }
13}
14
15 int main(int argc, char* argv[])
16 {
17  char *inputString = "Nahid";
18  printf("Input string : %s\n", inputString);
19  encryptString2(inputString);
20  printf("Input String : %s\n", inputString);
21 }

当我在 visual studio 中编译时,第 9 行会导致问题。它显示

Unhandled exception at 0x000B1AA4 in Page_182.exe: 0xC0000005: Access violation writing location 0x000B5C40.

谁能解释为什么会出现这个错误以及如何解决这个问题? 提前致谢。

不能修改字符串文字。任何修改字符串文字的尝试都会导致未定义的行为。

来自 C 标准(6.4.5 字符串文字)

7 It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.

改为使用字符数组。例如

char inputString[] = "Nahid";